【问题标题】:IE TextRange.HtmlText returns unexpected line breaksIE TextRange.HtmlText 返回意外的换行符
【发布时间】:2013-03-19 05:24:56
【问题描述】:

在一个空的 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 变量:如您所见,“Why”一词后有一个换行符

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 

【问题讨论】:

    标签: javascript asp.net textselection selectedtext


    【解决方案1】:

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

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

    或者像这样使用javascript:

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

    【讨论】:

    • 我知道,但我想弄清楚问题的根源是什么
    猜你喜欢
    • 2017-01-26
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多