0

在一个空的 Asp.Net 项目中,我有以下代码:

我在 IE 8 中使用 TextRange.HtmlText 来检索用户在 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 
4

1 回答 1

0

我不知道它为什么这样做,但你可以用 C# 像这样修复它:

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

或者像这样使用javascript:

myString = myString.replace("\r\n", "");
于 2013-03-18T21:01:06.510 回答