天天看點

jquery 移除click添加click事件

有事,我們在做web開發時,需要臨時移除某個節點的click事件,然後再某些條件下再加上click事件,那麼,這個需求該如何實作呢?首先,在html重定義click事件有兩種方式,針對這兩種方式有兩種移除、添加click事件的方法。

1、方式一:

第一種定義click事件的方法是在标簽内部加上onclick的屬性,代碼如下:

<input id = "demoId" type="button" οnclick="demoFunction();" value="單擊事件"/>      

針對這種定義方式,onclick是input标簽的一個屬性,是以移除這類click的方法是:

$("#demoId").removeAttr("onClick");      

添加click時間的方法:

$("#demoId").attr("onclick","demoFunction();");      

2、方式二:

第二種定義方式是在<script type="text/javascript">中綁定click事件,代碼如下:

$('#demoId').click(function(){
    alert("click");
});      

此種定義方法的移除可以通過接觸綁定的click事件來進行,如下:

$("#demoId").unbind("click");      
$('#demoId').bind('click', function() {/* Act on the event */ });      

繼續閱讀