开发者

Keep the selection in a textarea

Can I prevent the loss of selection in the "onblur" event?

<!DOCTYPE html>

<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en" lang = "en">
    <head>
        <meta http-equiv = "Content-Type" content = "text/html; charset=utf-8">

        <script type = "text/javascript">
            window.onload = function () {
                var textarea = document.getElementsByTagName (开发者_如何转开发"textarea")[0];

                textarea.onblur = function () {
                    alert ("Should keep selection");

                    return false;
                }
            }
        </script>

        <title>Select me!</title>
    </head>

    <body>
        <textarea>Select me!</textarea>
    </body>
</html>


I don't think that's a good idea. A user with mouse in his/her hand can click anywhere on the page. If you get him/her back into the textarea, it won't be following the principles of web accessibility.


Maybe you mean you want to remember the selection, even if the user focuses on another element, so that if they go back to the textarea, the same text will still be selected. Is this the case?

If so, I think the easiest thing would be to put the textarea in an iframe in the same domain. Each document maintains its own selection context. Of course, you'll need to read the data from the textarea, and probably copy it to a hidden field in your form, as you can't have fields in a form in another document, so you need to make a sort of proxy.


textarea.onblur = function () {
    alert("Should keep selection");
    textarea.focus();

    return false;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜