[英]removeAttr('id') clears #id but still the clicks work
我有<div id="SocialInteract">
$('#SocialInteract').click(function() {
$('#SocialCount').load('sc.php');
$('#TeacherAttendance').removeAttr('id');
});
當我查看InspectElement時, #id
已刪除,但仍然需要單擊。 我做錯了什么?
刪除id
不會刪除已附加的click
事件。 使用unbind()
方法刪除附加事件。
$('#TeacherAttendance').unbind("click");
編輯:
您應該使用off()
作為它的更新對象。 unbind()
仍然存在以實現向后兼容
$('#TeacherAttendance').off('click');
相關問題: 在jQuery中刪除事件處理程序的最佳方法?
您將事件綁定到DOM元素,然后刪除ID,但事件已注冊。
使用off或unbind或將全局變量設置為turnoff / remove / doNothing。
.click()
只是附加而忘記了。 不管該元素是否發生任何變化。
因此,您必須手動取消綁定事件。
.unbind('click');
要使事件綁定具有動態性,請將其攻擊document
並等待其冒泡:
$(document).on('click', #SocialInteract', function() {
$('#SocialCount').load('sc.php');
$('#TeacherAttendance').removeAttr('id');
});
這樣,事件便被注冊到document
節點:如果在具有該ID的元素上觸發了click事件,則它將觸發。 如果沒有,它不會。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.