【问题标题】:Removing event handlers of children删除孩子的事件处理程序
【发布时间】:2011-03-22 18:10:47
【问题描述】:
我正在尝试从所有 a 标记中取消绑定事件处理程序(单击),但不知何故它不起作用。你们知道为什么吗?
// Remove eventhandlers
row.find('a').each(function(){
$(this).unbind('click');
alert($(this).attr("onClick"));
});
它会一直输出当前的onClick函数。
谢谢
【问题讨论】:
标签:
jquery
events
event-handling
unbind
【解决方案1】:
$('a').unbind('click');
或
$('a').each(function() {
return false;
});
【解决方案2】:
jQuery 的.unbind() 仅删除由 jQuery 分配和维护的处理程序。您的内联处理程序不受影响。
如果要删除内联属性,请使用removeAttr()。
row.find('a').each(function(){
$(this).removeAttr('onClick');
alert($(this).attr("onClick"));
});
http://api.jquery.com/removeattr/