剥离HTML,保留mailto标记的换行符

Stripping HTML, preserving line breaks for mailto tag

本文关键字:换行符 mailto HTML 保留 剥离      更新时间:2023-09-26

我几乎从这里得到了投票最多的答案,但我正试图在mailto标签正文中放入div内容。

function getInnerText(el) {
    var sel, range, innerText = "";
    if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
        sel = window.getSelection();
        sel.selectAllChildren(el);
        innerText = "" + sel;
        sel.removeAllRanges();
    } else if (typeof document.selection != "undefined" && typeof document.body.createTextRange != "undefined") {
        range = document.body.createTextRange();
        range.moveToElementText(el);
        innerText = range.text;
    }
    return innerText;
}

function doMailTo() {
    var title = $('#title').val();
    var el = document.getElementById("container");
    //alert(getInnerText(el));  //--> works fine
    location.href = "mailto:?subject="+title+"&body="+(getInnerText(el));
}
<a href="javascript:doMailTo();">Email</a>

这在警告中很有效,但在电子邮件中会丢失换行符。有没有办法用%0A% 0A代替换行符?还是用另一种方式做同样的事情?

谢谢!

简短的回答,但是这个%0D%0A%0D%0A适合我。