问题描述:

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 following 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.

相关阅读:
Top