2013-03-18 59 views
0

在一個空Asp.Net的項目,我有以下代碼:IE TextRange.HtmlText返回意外換行符

我使用TextRange.HtmlText在IE 8來檢索DIV元素用戶的文本選擇。但是,由於某些原因,即使原始文本中沒有換行符,選定的html也包含\ r \ n。有誰知道爲什麼會發生這種情況?

如果我使用text屬性代替,則不會出現相同的行爲。

<script type="text/javascript"> 
    function OkClick() { 
     var TextRange = document.selection.createRange(); 
     var SelectedText = TextRange.htmlText; 
    } 
</script> 

<div> 
This is a test with a long line of text on a single line with no line breaks.Why is there a line break returned from htmlText on the TextRange object 
</div> 

<button onclick="OkClick()">OK</button> 

分配給我的SelectedText變量如下:正如你可以看到有字後換行「爲什麼」

This is a test with a long line of text on a single line with no line breaks.Why \r\nis there a line break returned from htmlText on the TextRange object 

回答

0

我不知道它爲什麼這樣做,但你可以用C#解決這個問題是這樣的:

myString = myString.Replace("\r\n", string.Empty); 

或類似這樣的JavaScript wtih:

myString = myString.replace("\r\n", ""); 
+0

我知道,但我想弄清楚問題的根源是什麼 – TGH 2013-03-18 21:02:54