問題描述
我們的 drupal 站點使用我們尚未升級的 jQuery 版本 1.2.1 運行.
Our drupal site runs with jQuery version 1.2.1 which we have not upgraded.
問題是這樣的:
我們需要添加一個名為 jQuery Tokeninput 的新插件,但它只適用于最新的 jQuery 版本.我們嘗試在舊版本中添加最新的 jQuery 版本,但結果很奇怪.
We need to add a new plugin named jQuery Tokeninput, but it's working only in latest jQuery versions. We tried adding the latest jQuery version with old version, but it produces weird results.
我的問題是,如何在不影響舊 jQuery 插件的情況下包含最新的 jQuery 文件?
My question is, how to include the latest jQuery file without affecting the old jQuery plugins?
推薦答案
方法一:(推薦)
你可以這樣做:
<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>
這樣,當你想要用新版本的 jquery 而不是 $
制作的東西時,請使用 $jq
.
And this way when you want something made with the new version of jquery instead of the $
use $jq
.
$jq('.selector').on('click', function(){
//do something
});
方法 #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);
}
}
確保只在您網站的前端執行此操作.如果它們依賴于 Drupal 的 jQuery 版本,它可能會弄亂管理工具欄.
這篇關于在頁面上包含兩個版本的 jQuery 而不會影響舊插件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!