![](/img/trans.png)
[英]How to add 2 onclick event methods for a single element which class has dynamically change on the first click event method on jquery
[英]how to unbind click event for element which has “onclick=xxx”?
如何為具有"onclick=xxx"
的元素取消單擊事件的綁定?
請參閱下面的代碼:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<button id="a" onclick="a()">a</button>
<button onclick="test()">test</button>
<script type="text/javascript">
function a(){
alert('a');
}
function test(){
// how can I remove the click function for #a?
// code bellow not works
$('#a').unbind('click');
}
</script>
您應該使用:
$("#a").removeAttr("onclick");
要么:
$("#a").prop("onclick", null);
您可以簡單地使用jQuery的prop()
方法從元素中刪除onclick
屬性:
$('#a').prop('onclick', null);
您不應使用Milind Anantwar的回答中所述的removeAttr()`,因為這在舊版本的IE中將無法正常工作:
注意:使用
.removeAttr()
刪除嵌入式onclick事件處理程序無法在Internet Explorer 6、7或8中獲得所需的效果。資料來源: jQuery的
removeAttr()
文檔 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.