gop*_*h.m 4 javascript jquery drupal jquery-plugins drupal-theming
我们的drupal站点运行jQuery版本1.2.1,我们还没有升级.
问题是这样的:
我们需要添加一个名为jQuery Tokeninput的新插件,但它只适用于最新的jQuery版本.我们尝试使用旧版本添加最新的jQuery版本,但它会产生奇怪的结果.
我的问题是,如何在不影响旧的jQuery插件的情况下包含最新的jQuery文件?
方法#1 :(推荐)
你可以这样做:
<script type='text/javascript' src='js/jquery_1.7.1.js'></script>
<script type='text/javascript'>
// In case you wonder why we pass the "true" parameter,
// here is the explanation:
// - When you use jQuery.noConflict(), it deletes
// the "$" global variable.
// - When you use jQuery.noConflict(true), it also
// deletes the "jQuery" global variable.
var $jq = jQuery.noConflict(true);
</script>
<script type='text/javascript' src='js/jquery_1.2.1.js'></script>
Run Code Online (Sandbox Code Playgroud)
这种方式当你想要使用新版本的jquery而不是$使用它时$jq.
$jq('.selector').on('click', function(){
//do something
});
Run Code Online (Sandbox Code Playgroud)
方法#2 :(可能会破坏您网站上的内容 - 不推荐)
在您的template.php文件中:
<?php
function {theme_name}_preprocess(&$vars, $hook) {
if (arg(0) != 'admin' && $hook == "page") {
// Get an array of all JavaScripts that have been added
$javascript = drupal_add_js(NULL, NULL, 'header');
// Remove the original jQuery library
unset($javascript['core']['misc/jquery.js']);
// Add in our new jQuery library
// We do it this way to keep the includes in the same order
$core = array(
//Alternative jQuery
drupal_get_path('theme', '{theme_name}').'/js/libs/jquery-1.7.1.min.js' => array(
'cache' => TRUE,
'defer' => FALSE,
)
);
// Merge back into the array of core JavaScripts
$javascript['core'] = array_merge($javascript['core'], $core);
// Rerender the block of JavaScripts
$vars['scripts'] = drupal_get_js(NULL, $javascript);
}
}
Run Code Online (Sandbox Code Playgroud)
请务必仅在您网站的前端执行此操作.如果它们依赖于Drupal的jQuery版本,它可能会弄乱管理工具栏.