在页面上包含两个版本的 jQuery 而不会影响旧插件 [英] Include two versions of jQuery on a page without affecting old plugins

查看:14
本文介绍了在页面上包含两个版本的 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 而不会影响旧插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆