Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery Onclick被调用了好几次,即使我只调用过一次_Jquery_Onclick_Toggle - Fatal编程技术网

Jquery Onclick被调用了好几次,即使我只调用过一次

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

我已将此代码放在jQuery中的
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()