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 站点,当您单击切换它时 hides/shows 元素,它在 JSFiddle 上工作,但在 Drupal 上不工作?
我想,也许 JS 注入器是问题所在,所以我 运行 另一个简单的测试,例如
(function($){
Drupal.behaviors.mymodule = {
attach: function(context, settings) {
$("html, body").animate({ scrollTop: $('#buttons').offset().top }, 1000);
}
};
})(jQuery);
而且效果很好!
为什么切换脚本不起作用?
(我将其发布在 Drupal answers 上,他们将其搁置以备不时之需并定向到此处)
我认为您应该在查询 DOM 时使用 context
。此外,使用 .once()
将确保您的点击处理程序仅附加一次(因此,运行):
attach: function(context, settings) {
$(".myelement", context).once('click-slide-toggle').click(function() {
$('.another-element p', context).toggle("slide", { direction: "down" }, 1000);
});
}
我得到了这个脚本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 站点,当您单击切换它时 hides/shows 元素,它在 JSFiddle 上工作,但在 Drupal 上不工作?
我想,也许 JS 注入器是问题所在,所以我 运行 另一个简单的测试,例如
(function($){
Drupal.behaviors.mymodule = {
attach: function(context, settings) {
$("html, body").animate({ scrollTop: $('#buttons').offset().top }, 1000);
}
};
})(jQuery);
而且效果很好!
为什么切换脚本不起作用?
(我将其发布在 Drupal answers 上,他们将其搁置以备不时之需并定向到此处)
我认为您应该在查询 DOM 时使用 context
。此外,使用 .once()
将确保您的点击处理程序仅附加一次(因此,运行):
attach: function(context, settings) {
$(".myelement", context).once('click-slide-toggle').click(function() {
$('.another-element p', context).toggle("slide", { direction: "down" }, 1000);
});
}