【问题标题】:Why getting a text selection from textarea works when a button clicked but not when a "div" clicked (in Internet Explorer)为什么单击按钮时从 textarea 获取文本选择有效,但单击“div”时无效(在 Internet Explorer 中)
【发布时间】:2011-11-03 16:08:18
【问题描述】:

考虑以下代码:(Live demo here - Open in Internet Explorer 7 or 9)

HTML:

<textarea>Hello Stack Overflow</textarea>
<input class="a" type="button" value="Click here does the job" />
<div class="a">But clicking here not :(</div>

JS:

    function getInputSelection(el) {
    var start = 0, end = 0, normalizedValue, range, textInputRange, len, endRange;

    if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
        start = el.selectionStart;
        end = el.selectionEnd;
    } else {
        range = document.selection.createRange();

        if (range && range.parentElement() == el) {
            len = el.value.length;
            normalizedValue = el.value.replace(/\r\n/g, "\n");

            // Create a working TextRange that lives only in the input
            textInputRange = el.createTextRange();
            textInputRange.moveToBookmark(range.getBookmark());

            // Check if the start and end of the selection are at the very end
            // of the input, since moveStart/moveEnd doesn't return what we want
            // in those cases
            endRange = el.createTextRange();
            endRange.collapse(false);

            if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
                start = end = len;
            } else {
                start = -textInputRange.moveStart("character", -len);
                start += normalizedValue.slice(0, start).split("\n").length - 1;

                if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
                    end = len;
                } else {
                    end = -textInputRange.moveEnd("character", -len);
                    end += normalizedValue.slice(0, end).split("\n").length - 1;
                }
            }
        }
    }

    return {start: start, end: end};
}


function getselection() {
  var selectedText = getInputSelection($("textarea")[0]);
  var start = selectedText.start;
  var end = selectedText.end;
  alert("Start: " + start + ", End: " + end);
}

$(".a").click(getselection);

getInputSelection() 函数取自 here

由于某种原因,当点击&lt;div&gt; 时,结果总是:

Start: 0, End: 0

有什么办法解决这个问题吗?

【问题讨论】:

  • 使用 jsfiddle 似乎两者都不起作用:jsfiddle.net/nayish/cKt8e
  • 您必须在文本区域中选择一些文本(或移动光标),然后单击该元素。
  • 在这种情况下它对我来说很好,你在什么浏览器上运行它?我在 chrome 上运行它。

标签: javascript jquery internet-explorer textarea textselection


【解决方案1】:

问题是在getInputSelection() 函数执行之前单击&lt;div&gt; 会失去对文本区域的关注。有两种选择:

【讨论】:

  • @Misha:unselectable 的一个警告:它仅适用于该元素而不适用于子元素。见stackoverflow.com/questions/826782/…
  • 谢谢,您的回答帮助很大。谢谢。我不知道你从哪里得到这些知识)
【解决方案2】:

您的问题是由资源管理器引起的。当您单击按钮时,焦点不会从输入字段中丢失,但是当您单击 div 时,焦点会被删除,因此不再选择所选文本。

您要做的是找到一种方法来克服这个问题,方法是强制单击 div 以不从 input 移除焦点。

您最好在每次更改时保存开始和结束值,这样当您单击它们时,无论是否选中它都无关紧要。

希望这有帮助:)

【讨论】:

    猜你喜欢
    • 2015-09-12
    • 2012-12-03
    • 2015-10-19
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多