为什么从单击按钮时从textarea获取文本选择,而不是在“div”按钮时。单击(在Internet Explorer中) [英] Why getting a text selection from textarea works when a button clicked but not when a "div" clicked (in Internet Explorer)

查看:110
本文介绍了为什么从单击按钮时从textarea获取文本选择,而不是在“div”按钮时。单击(在Internet Explorer中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:(此处实时演示 - 在Internet Explorer 7或9中打开

Consider the following code: (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()函数取自此处

出于某种原因,当<单击code>< div> ,结果始终为:

For some reason, when the <div> is clicked, the result is always:

Start: 0, End: 0

任何想法如何解决?

推荐答案

问题是点击< div> 在<$ c之前失去了对textarea的关注$ c> getInputSelection()函数执行。有两种选择:

The problem is that clicking the <div> loses the focus on the textarea before the getInputSelection() function executes. There are two alternatives:

  • Either use the mousedown event instead: http://jsfiddle.net/RbSLw/13/, or
  • Make the <div> unselectable (done by adding a unselectable="on" attribute in IE): http://jsfiddle.net/RbSLw/14/.

这篇关于为什么从单击按钮时从textarea获取文本选择,而不是在“div”按钮时。单击(在Internet Explorer中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆