获得可信的div文本时保留换行符

ext*_*ice 5 html dom

我想将文本保存在一个被pre格式化的contenteditable div中.我如何获得pre文本的形式而不是文本的位置\n\r省略?

$('#save').click(function(e) {
    var id = "board_code";
    var ce = $("<pre />").html($("#" + id).html());
        if ($.browser.webkit)
          ce.find("div").replaceWith(function() { return "\n" + this.innerHTML; });
        if ($.browser.msie)
          ce.find("p").replaceWith(function() { return this.innerHTML + "<br>"; });
        if ($.browser.mozilla || $.browser.opera || $.browser.msie)
          ce.find("br").replaceWith("\n");

        alert( ce.text() );
});
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/AD5q7/10/这不起作用

尝试字符串为contenteditable div

更新:通过键入来尝试字符串.粘贴字符串时可能没有问题.

1

abc def

    gh  i

jkl
Run Code Online (Sandbox Code Playgroud)

2

#include<iostream.h>
#include<conio.h>

int main(){
    int grade, passingMark=75;

    cout<<"Hi there, please enter your mark: ";
    cin>>grade;

    if( ((grade >= passingMark)||(grade==35)) && (grade<101)){
        cout<<"\nPass!";
    }

    return 0;//15lines
}    
Run Code Online (Sandbox Code Playgroud)

保存文件的格式也必须如此,并且不能删除\n\r \n.我希望警报应该包括\n

Saf*_*ain -2

尝试这个

alert( ce.text().replace(/\n/g, "\\n" ).replace(/\r/g, "\\r"));
Run Code Online (Sandbox Code Playgroud)