开发者

jQuery document delete key

I attached the delete key to document so when delete key is pressed, div gets removed. However when i'm inside a textarea, i don't want this behavior. The delete key inside textarea should only delete text and not affect the document and deleting the div. The 开发者_如何学Cfollowing is not working;

$(document).keyup(function() {
    if (e.which == 46) {
        $('div').remove()
    }
})

Check http://jsfiddle.net/a3Vdu/2/


A check against the type of element that triggered the event will work.

if (/input|textarea/i.test(e.target.tagName)) {
     return;   
}

Here's it added to your example http://jsfiddle.net/JY46t/


You need to bind a handler to the textarea to capture the event before it bubbles to the document. Write a similar function for your texarea:

$("#mytextarea").keyup(function(e) {}, false);

The inclusion of 'false' prevents event bubbling.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜