简体   繁体   English

删除除一个元素外的所有事件

[英]Remove all the events attached to an element except one

How may I remove all the events attached to a DOM element except one using jquery? 如何使用jquery删除除DOM元素之外的所有事件?

For example, I have a drop down <select> and it has various events attached to it (say click, double click, change etc). 例如,我有一个下拉<select>并附加了各种事件(例如单击,双击,更改等)。 What I want to achieve is to just have change event applied to it and remove all the other events. 我要实现的只是将change事件应用于它,并删除所有其他事件。 How may I achieve this? 我该如何实现?

PS I don't know about the events attached to the element PS我不知道该元素附带的事件

$( "#foo").unbind( "click" );

Fill in the rest. 填写其余部分。 http://api.jquery.com/unbind/ http://api.jquery.com/unbind/

$( "#foo").off();

Will remove all events attached to foo element 将删除附加到foo元素的所有事件

You can also do: 您也可以:

 $( "#foo").off("click dblclick");

you can then attach on foo element only onchange event 然后可以仅在onchange事件上附加foo元素

$("#foo").on("change",function(){
   //Do stuff here
})

May be you can use something like: 可能您可以使用类似以下的内容:

$.(selector).on('*',function(e)
{
 if(e.which='value_for_your_event_eg._14_for_tab')
 return true;
 else
 return false;
})

I don't know, I'm not an expert, but I guess this would work. 我不知道,我不是专家,但是我想这会起作用。

$._data(elem,'events') is not a method publicly supported: $._data(elem,'events')不是公开支持的方法:

DEMO 演示

$.each($._data($('select').get(0),'events'),function(k){
    if(k !== "change")
        delete $._data($('select').get(0),'events')[k];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM