Jquery Onclick被调用了好几次,即使我只调用过一次
我已将此代码放在jQuery中的Jquery Onclick被调用了好几次,即使我只调用过一次,jquery,onclick,toggle,Jquery,Onclick,Toggle,我已将此代码放在jQuery中的resize函数中。但是这个单击函数被多次调用。当我点击所需链接时,切换会发生很多次 if ($(window).width() <=768){ if ($('body').hasClass('page-search-ads')){ if ($('#-clasifika-results-simple-search-form img').hasClass('funnel')) { } else
resize
函数中。但是这个单击函数被多次调用。当我点击所需链接时,切换会发生很多次
if ($(window).width() <=768){
if ($('body').hasClass('page-search-ads')){
if ($('#-clasifika-results-simple-search-form img').hasClass('funnel')) {
} else {
$('#-clasifika-results-simple-search-form').append("<img class='funnel' src='" + Drupal.settings.basePath + "sites/all/themes/clasifika/images/filter.png'/>");
}
$('.funnel').click(function(){
$('.vehicle-cat, .vehicle-brand, .city-name-filter, .vehicle-mileage,.overall-cat,.city-name,.boat-bed,.boat-type,.boat-brand,.nautical-length,.overall-year,.airplane-type,.fashion-cat,.airplane-brand,.airframe-time,.propeller-hours,.monthly-salary,.amount-slider,.area-slider').slideToggle();
console.log("funnel click");
});
}
}
if($(window).width()问题在于resize()
事件对于调整窗口大小的每个像素触发一次。因此,在调整大小时,您将附加多个单击
处理程序。您只需将单击
移动到调整大小
处理程序之外,并使用委派的事件处理程序。尝试以下操作:
$(window).resize(function() {
if ($(window).width() <= 768 && $('body').hasClass('page-search-ads') && !$('#-clasifika-results-simple-search-form img').hasClass('funnel')) {
$('#-clasifika-results-simple-search-form').append("<img class='funnel' src='" + Drupal.settings.basePath + "sites/all/themes/clasifika/images/filter.png'/>");
}
});
$('#-clasifika-results-simple-search-form').on('click', '.funnel', function(){
$('.vehicle-cat, .vehicle-brand, .city-name-filter, .vehicle-mileage, .overall-cat, .city-name, .boat-bed, .boat-type, .boat-brand, .nautical-length, .overall-year, .airplane-type, .fashion-cat, .airplane-brand, .airframe-time, .propeller-hours, .monthly-salary, .amount-slider, .area-slider').slideToggle();
console.log("funnel click");
});
$(窗口)。调整大小(函数(){
if($(窗口).width()