我有一个模块,我在其中添加了 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);
使用weight
和scope
你应该能够确保你的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/