在页面上包含两个版本的 jQuery 而不会影响旧插件

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)