禁用特定页面上的jQuery函数

时间:2016-06-08 15:37:32

标签: jquery wordpress

我有一个jQuery函数,它显示一个灯箱弹出窗口:

      function flatsomeQuickView(element) {
         $(element).click(function(e){
             $(this).after('<div class="ux-loading dark"></div>');
               var product_id = $(this).attr('data-prod');
               var data = { action: 'ux_quickview', product: product_id};
                $.post(ajaxURL.ajaxurl, data, function(response) {
                 $.magnificPopup.open({
                    removalDelay: 300,
                    items: {
                      src: '<div class="product-lightbox">'+response+'</div>',
                      type: 'inline'
                    }
                  });
                   $('.ux-loading').remove();
                   $('.product-lightbox .product-gallery-slider').flickity({
                            cellAlign: "center",
                            wrapAround: true,
                            autoPlay: false,
                            prevNextButtons:true,
                            percentPosition: true,
                            imagesLoaded: true,
                            lazyLoad: 1,
                            pageDots: false,
                            rightToLeft: false
                   });
                   setTimeout(function() {

                        // Run Variations Form Scripts
                        if ($('.product-lightbox form').hasClass('variations_form')) {
                          $('.product-lightbox form.variations_form').wc_variation_form();
                        }

                        $(".product-lightbox form.variations_form").on( "show_variation", function (event, variation) {
                          if(variation.image_src){
                            $('.product-lightbox .product-gallery-slider .slide.first img').attr('src',variation.image_src);
                            $('.product-lightbox .product-gallery-slider .slide.first a').attr('href',variation.image_link);
                            $('.product-lightbox .product-gallery-slider').flickity( 'select', 0);
                          }
                        });

                        // Create QTY Buttons
                        $('.product-lightbox').addQty();

                  }, 600);
                });

                e.preventDefault();
            }); // product lightbox
      }
  flatsomeQuickView('.quick-view');

我不想在主页(Wordpress网站)上执行。为此,我在functions.php文件中创建了一个WP动作

function quick_view_override() {

global $myvar;

if($myvar == 'home') {
echo <<<EOT
<script>
function flatsomeQuickView(element) {
    $(element).click(false);
}
</script>
EOT;
}
}

add_action( 'wp_footer', 'quick_view_override', 100 );

但是.click()函数没有禁用flatsomeQuickView

2 个答案:

答案 0 :(得分:0)

jQuery&#39; click.on的快捷方式)不带布尔值(see docs)。

如果您在该元素上禁用所有点击处理程序,则使用$(element).off('click');

答案 1 :(得分:0)

根据RwwL的答案让它工作,但最终的代码要复杂得多:

function quick_view_override() {

global $myvar;

if($myvar == 'home') {
echo <<<EOT
<script>
function flatsomeQuickView(element) {
    jQuery(element).off('click');
}

flatsomeQuickView('.quick-view');

  jQuery('.woocommerce').arrive(".quick-view", function() {
    flatsomeQuickView(jQuery(this));
   });


</script>
EOT;
}
}

add_action( 'wp_footer', 'quick_view_override', 100 );