簡體   English   中英

從單個Div元素中刪除click事件

[英]Remove the click event from a single Div element

我已經將mousedown處理程序添加到父div內的所有div中,如下所示:

$productContainer.children(".button").on('mousedown', function(){
    var index = $(this).index();
    console.log("Clicked button at " + index);
    // this doesnt work
    $(this).removeAttr('mousedown');
});

現在,我想為該特定div單擊按鈕后刪除mousedown處理程序。 我怎么做? 刪除attr mousedown似乎無效。

使用off方法:

$productContainer.children(".button").on('mousedown', function(){
    var index = $(this).index();
    console.log("Clicked button at " + index);
    $(this).off('mousedown');
});

更多信息: http//api.jquery.com/off/

您也可以改用one方法。 它將在第一次觸發時自動刪除事件處理程序:

$productContainer.children(".button").one('mousedown', function(){
    var index = $(this).index();
    console.log("Clicked button at " + index);
});

更多信息: http : //api.jquery.com/one/

使用.off描述:刪除事件處理程序。

$productContainer.children(".button").off('mousedown');
$productContainer.children(".button").on('mousedown', function(){
    var index = $(this).index();
    console.log("Clicked button at " + index);
    // this doesnt work
    $(this).removeAttr('mousedown');
});

代替 :

$(this).removeAttr('mousedown');

采用 :

$(this).unbind('mousedown');

要么

$(this).off('mousedown');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM