Javascript Drupal 7在自定义主题中覆盖jquery js文件
是否可以重写/覆盖自定义模板脚本变量中使用的默认Drupal 7.26 jquery 我指的是其中一个js文件:Javascript Drupal 7在自定义主题中覆盖jquery js文件,javascript,php,jquery,drupal,drupal-7,Javascript,Php,Jquery,Drupal,Drupal 7,是否可以重写/覆盖自定义模板脚本变量中使用的默认Drupal 7.26 jquery 我指的是其中一个js文件: 由一个来自自定义主题 我在sites/all/MYTPL/template.php中尝试过这个方法,但不起作用: $scripts['misc/jquery.js']['data']=$base\u theme'/js/packages/jquery-2.1.0.min.js' 我想知道是否有人在不使用任何模块(如jqueryupdate)的情况下管理了它 ==更新==== 事实上
由一个来自自定义主题
我在sites/all/MYTPL/template.php
中尝试过这个方法,但不起作用:
$scripts['misc/jquery.js']['data']=$base\u theme'/js/packages/jquery-2.1.0.min.js'代码>
我想知道是否有人在不使用任何模块(如jqueryupdate
)的情况下管理了它
==更新====
事实上,我在@cojomojo的答案的基础上做了一些修改,通过接受答案解决了这个问题:
function THEMEname_js_alter(&$javascript) {
// Swap out jQuery to use an updated version of the library.
$javascript['misc/jquery.js']['data'] = drupal_get_path('theme', 'THEMEname') . '/js/packages/jquery-2.1.0.min.js';
}
也许用钩子吧。
请记住,其他模块将依赖于jQuery,如果它们使用在不同版本之间发生更改的内容,您可能会遇到问题。因此,您有一些选项可以执行此操作。这是:
您可以像这样使用hook_js_alter:
<?php
function hook_js_alter(&$javascript) {
// Swap out jQuery to use an updated version of the library.
$javascript['misc/jquery.js']['data'] = drupal_get_path('module', 'jquery_update') . '/jquery.js';
}
?>
或者,为了避免像前面提到的2pha那样的破坏,您可以并排运行两个版本的jquery。jQuery已经具备了与不同版本的jQuery一起运行的功能(或者,实际上,与使用$symbol作为函数或变量的任何其他JavaScript库一起运行)。这是noConflict()函数。您可以在此处查看API页面:
要在项目中使用此功能,只需告诉浏览器您的新jQuery库,并通过自定义noConflict标识符引用它。下面是一个如何在自定义主题中实现这一点的示例
MyTheme/page.tpl.php
<head>
<title><?php print $head_title; ?></title>
<?php print $head; ?>
<?php print $styles; ?>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
var $jq = jQuery.noConflict();
</script>
<?php print $scripts; ?>
</head>
var$jq=jQuery.noConflict();
另一种可能的方法是将jQuery与预处理页面交换,如下所示:
<?php
function yourModuleOrThemeName_preprocess_page(&$variables) {
// exclude backend pages to avoid core js not working anymore
// you could also just use a backend theme to avoid this
if (arg(0) != 'admin' || !(arg(1) == 'add' && arg(2) == 'edit') || arg(0) != 'panels' || arg(0) != 'ctools') {
$scripts = drupal_add_js();
$new_jquery = array(drupal_get_path('theme', 'YOURTHEME') . '/js/jquery-1.7.1.min.js' => $scripts['core']['misc/jquery.js']);
$scripts['core'] = array_merge($new_jquery, $scripts['core']);
unset($scripts['core']['misc/jquery.js']);
$variables['scripts'] = drupal_get_js('header', $scripts);
}
}
?>
对我来说,最好的方法是并排运行两个版本的jQuery(Drupal7默认使用的版本以及主题所需的任何版本),或者将jQuery与预处理页面交换。hook_js_alter有可能破坏任何需要不同版本jQuery的东西。所有可能的方法都在这里:在主题的模板.php
文件中使用此代码
hook\u js\u alter
仅适用于jQuery更新模块,不是吗?cojomojo有您想要的详细答案我正在使用hook\u js\u alter()
中的第一个代码,但它不起作用,我应该在声明后立即调用该函数,对吗?如果是这样,为什么在该函数中有drupal\u get\u path('module','jquery\u update')
?我的jquery.js
文件位于/sites/all/themes/MYTPL/js/jquery-2.1.0.min.js
@aspirinemaga查看这里:还有:
function [YOUR_THEME]_js_alter(&$js)
{
$jqKey = "my-new-jquery"; // a key for new jquery file entry
$js[$jqKey] = $js['misc/jquery.js']; // copy the default jquery settings, so you don't have to re-type them.
$js[$jqKey]['data'] = "https://code.jquery.com/jquery-2.1.0.min.js"; // the path for new jquery file.
$js[$jqKey]['version'] = '2.1.0'; // your jquery version.
unset($js['misc/jquery.js']); // delete drupal's default jquery file.
}