简体   繁体   English

removeAttr('id')清除#id,但点击仍然有效

[英]removeAttr('id') clears #id but still the clicks work

I have the <div id="SocialInteract"> 我有<div id="SocialInteract">

$('#SocialInteract').click(function() {

$('#SocialCount').load('sc.php');
$('#TeacherAttendance').removeAttr('id');
});

When I view the InspectElement the #id is removed but it still takes the click. 当我查看InspectElement时, #id已删除,但仍然需要单击。 Something I am doing wrong? 我做错了什么?

Your click event still fires because the event is already attached to the TeacherAttendance element. 由于该事件已附加到TeacherAttendance元素,因此您的click事件仍然会触发。

If you want to detach the click event, you should use off() or unbind() : 如果要分离click事件,则应使用off()unbind()

$('#TeacherAttendance').off('click');
$('#TeacherAttendance').unbind('click');

Removing id wont remove already attached click event. 删除id不会删除已附加的click事件。 Use unbind() method to remove attached event. 使用unbind()方法删除附加事件。

$('#TeacherAttendance').unbind("click");

EDIT : 编辑:

You should use off() as its the updated one. 您应该使用off()作为它的更新对象。 unbind() is still there for backword compatibility unbind()仍然存在以实现向后兼容

$('#TeacherAttendance').off('click');

Related question : Best way to remove an event handler in jQuery? 相关问题: 在jQuery中删除事件处理程序的最佳方法?

You are binding to the DOM element the event, then you remove the id but the event it's already registered. 您将事件绑定到DOM元素,然后删除ID,但事件已注册。

use off or unbind or set a global var to turnoff/remove/doNothing . 使用off或unbind或将全局变量设置为turnoff / remove / doNothing。

.click() just attaches and forgets. .click()只是附加而忘记了。 It doesn't care whether anything happens to that element. 不管该元素是否发生任何变化。

So you've to manually unbind the event. 因此,您必须手动取消绑定事件。

.unbind('click');

To make the event binding dynamic, attack it to the document and wait for it to bubble up: 要使事件绑定具有动态性,请将其攻击document并等待其冒泡:

$(document).on('click', #SocialInteract', function() {

$('#SocialCount').load('sc.php');
$('#TeacherAttendance').removeAttr('id');
});

This way the event is registered to the document node: if the click event was fired on an element that had that ID, it will trigger. 这样,事件便被注册到document节点:如果在具有该ID的元素上触发了click事件,则它将触发。 If not, it won't. 如果没有,它不会。

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

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