简体   繁体   English

为什么单击按钮而不是单击“ div”时,可以从textarea中选择文本(在Internet Explorer中)

[英]Why getting a text selection from textarea works when a button clicked but not when a “div” clicked (in Internet Explorer)

Consider the following code: ( Live demo here - Open in Internet Explorer 7 or 9 ) 考虑以下代码:( 此处为实时演示-在Internet Explorer 7或9中打开

HTML: 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: 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);

The getInputSelection() function is taken from here . getInputSelection()函数从此处获取

For some reason, when the <div> is clicked, the result is always: 由于某些原因,当单击<div> ,结果始终为:

Start: 0, End: 0

Any ideas how to fix that ? 任何想法如何解决?

The problem is that clicking the <div> loses the focus on the textarea before the getInputSelection() function executes. 问题在于,在执行getInputSelection()函数之前,单击<div>失去对文本区域的关注。 There are two alternatives: 有两种选择:

Your problem is caused by explorer. 您的问题是由资源管理器引起的。 When you click on a button the focus is not lost from the input field but when you click on the div it removed and hence the selected text is no longer selected. 当您单击按钮时,焦点不会从输入字段中丢失,但是当您单击div时,焦点将被删除,因此不再选择所选文本。

What you want to do is find a way to overcome this by forcing clicking on a div to not remove the focus from the input . 您想要做的就是找到一种方法来解决此问题,方法是强制单击div而不将焦点从input移除。

You may be better off simply saving the start and end values everytime they change and this way when you click them it won't matter if it is selected or not. 最好在每次更改起始值和结束值时都保存它们,这样,单击时就不必选择是否相关了。

Hope this helped :) 希望这对您有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM