2013-10-05 37 views
4

我想將文本保存在格式化爲pre的contenteditable div中。我如何得到pre形式的文本,而不是\n\r被省略的文本?保留換行符時獲得contenteditable div文本

$('#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()); 
}); 

http://jsfiddle.net/AD5q7/10/這並不工作

嘗試字符串CONTENTEDITABLE格

UPDATE:通過鍵入嘗試的字符串。字符串粘貼時可能沒有問題。

abc def 

    gh i 

jkl 
#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 
}  

的保存文件必須也格式化這樣並沒有\ n \ r去除。林預計該警示應包括\ n

+1

它適用於我......問題到底在哪裏? – geomagas

+0

對不起,我不確定我完全理解。我可以得到一個例子,說明你把什麼放入div。什麼是提醒。然後你想看到提醒。 – Trevor

+0

嘗試第一個字符串。該警報還應該包含換行符,而不是\ n已刪除 – extraRice

回答

0

試試這個

alert(ce.text().replace(/\n/g, "\\n").replace(/\r/g, "\\r")); 
0

當contenteaditable格失去焦點,整個文本被轉換後,它轉換失去焦點的HTML如

<div contenteditable="true">Your text is here 
and has new line </div> 

虛擬textarea到html即

<div>Your text is here</div><br><div>and has new line </div> 

當你試圖.text( ),你會失去理想的對齊,因爲實際上不再存在於該div中。

解決方案 1.您可以使用文本區域,設置爲0邊框屬性這將使它看起來像一個CONTENTEDITABLE股利或 2.你可以抓住CONTENTEDITABLE div的整個HTML,並與替換HTML相應的文本表示使用JavaScript(爲此參考javascript regex replace html chars

相關問題