jquery - drupal 7 add_js 权重

标签 jquery drupal drupal-7

我有一个模块,我在其中添加了 js

if($_GET['q'] == 'workpage') {
drupal_add_js(drupal_get_path('module', 'sb_carousel').'/js/slide.js');
}

问题是它将它添加到 .info 文件中定义的 jqueru 之上。我看到 js 文件底部有一个“weight”属性,如何获取我的 js??

最佳答案

您可以通过将 $options 数组传递到 drupal_add_js() 来设置权重:

$file = drupal_get_path('module', 'sb_carousel').'/js/slide.js';
$options = array(
  'weight' => 1000, // High number to push this file to the bottom of the list
  'scope' => 'footer' // This will output the JS file in the footer scope, so at the end of the document
);

drupal_add_js($file, $options);

使用weightscope你应该能够确保你的JS文件是HTML中最后输出的。

更新

只是一个想法,你提到你的 jQuery 文件是从 .info 文件加载的......没有必要这样做,jQuery 已经默认添加到 Drupal 中。您可以使用 jQuery Update module 升级到 jQuery 1.5但目前没有(官方)支持任何更高版本。

如果您正在加载第二个 jQuery 文件,这可能是首先导致问题的原因,除非您使用 jQuery.noConflict()正确地,即使这样你可能仍然会遇到问题。

关于jquery - drupal 7 add_js 权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8330757/

相关文章:

css - 样式化 Drupal 的打印模块 PDF

javascript - 如何根据与变量匹配的数据属性选择div?

javascript - 如何使用 jquery 将值传递给隐藏字段?

html - 我可以在项目包装器级别而不是项目标签指定 Schema.org 属性吗?

drupal - drupal中的尾部斜杠

drupal - 如何在 drupal 7 中使用自定义数据类型作为字段?

javascript - jquery google visual api graph 的数据行不起作用

jquery - 2 个并排的 div 框,宽度为 50%,高度为 100%,悬停在每个框上时扩展到 100% 填满整个宽度

jquery - Drupal/jQuery/CSS::Wrap 单选按钮标签在 span 标签中?

php - 是否可以通过javascript调用自定义模块中的drupal函数?