开发者

Is there a JavaScript event that is triggered when a text selection is cleared?

开发者 https://www.devze.com 2023-03-12 07:03 出处:网络
I am using the JavaScript select event to detect when the user selects a range of characters within a textarea. However, the event does not seem to be invoked when the selection is cleared, either by

I am using the JavaScript select event to detect when the user selects a range of characters within a textarea. However, the event does not seem to be invoked when the selection is cleared, either by clicking elsewhere in the tex开发者_如何学运维tarea, or by moving the caret.

I have tested this in Chrome 12 and Firefox 4


There's onblur event which you can append after a selection has being made :)

var t = document.getElementsByTagName('textarea')[0];
t.onselect = function() {
    alert('select');
    if (typeof(t.onblur) !== 'function') {
        t.onblur = function() {
            alert('selection cleared');
            t.onblur = null;
        }
    }
}

jsFiddle

My idea: create a function which will be executed after the text has been selected, and after it's being executed unset it ... since there's no text selection any more, and when the text has been selected again - the function will be defined again


How about a combination of click, blur, and keypress event bound to the element?

Here is a demo of JavaScript select/deselect event

0

精彩评论

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