繁体   English   中英

jQuery脚本不适用于Drupal

[英]jQuery script not working on Drupal

我得到了这个脚本http://jsfiddle.net/j999y/2878/

(function($){
  Drupal.behaviors.mymodule = {
    attach: function(context, settings) {
      $( ".myelement" ).click(function() {     
         $('.another-element p').toggle("slide", { direction: "down" }, 1000);
      });
    }
  };
})(jQuery);

通过JS Injector模块注入到我的Drupal网站中,当您单击Toggle时,它隐藏/显示了该元素,它可以在JSFiddle上运行,但不能在Drupal上运行?

我认为,也许是JS Injector是问题所在,所以我进行了另一个简单的测试,例如

(function($){
  Drupal.behaviors.mymodule = {
    attach: function(context, settings) {
     $("html, body").animate({ scrollTop: $('#buttons').offset().top }, 1000);
    }
  };
})(jQuery);

而且效果很好!

为什么切换脚本不起作用?

(我将其发布在Drupal答案上,他们将其搁置为脱位并定向到此处)

我认为您在查询DOM时应使用context 同样,使用.once()将确保单击处理程序仅被附加一次(因此,正在运行):

attach: function(context, settings) {
  $(".myelement", context).once('click-slide-toggle').click(function() {
     $('.another-element p', context).toggle("slide", { direction: "down" }, 1000);
  });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM