如何禁用仅一个元素的JQuery按键事件? [英] How to disable JQuery keypress event for just one element?

查看:110
本文介绍了如何禁用仅一个元素的JQuery按键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $(document).keypress(myhandler ); 

我处理空格键滚动列表。



麻烦的是,有一个< input type ='text'/> 元素,我不希望'空格'按键在输入中滚动列表。



我在JQuery传递给处理程序的事件对象中找不到任何信息,以确定哪里事件的来源是。

解决方案

或者,您可以将另一个事件处理程序附加到输入字段,并在此处理程序中停止事件的传播:

  jQuery('#input-field-id')bind('keypress',function ){
e.stopPropagation();
});

这样,你可以保留全局事件处理程序。可能会更干净。


On my site I have registered a keypress event handler for the whole document.

$(document).keypress(myhandler);

And I handle the 'space' key to scroll a list.

Trouble is, there is an <input type='text' /> element, and I don't want 'space' keypress to scroll the list when it is entered in the input.

I couldn't find any information in the "event" object passed by JQuery to the handler, to identify where the source of the event is.

解决方案

Alternatively, you could attach another event handler to the input field, and in this handler stop the propagation of the event:

jQuery('#input-field-id').bind('keypress', function(e) {
    e.stopPropagation(); 
});

This way, you can leave the global event handler as it is. Might be cleaner.

这篇关于如何禁用仅一个元素的JQuery按键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆