[英]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.