开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜