Include two versions of jQuery on a page without affecting old plugins(在页面上包含两个版本的 jQuery 而不会影响旧插件)
问题描述
我们的 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 而不会影响旧插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在页面上包含两个版本的 jQuery 而不会影响旧插件


基础教程推荐
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01