jQuery only allow one version

时光总嘲笑我的痴心妄想 提交于 2019-12-10 21:44:01

问题


I have a third party script that is including jquery (which causes things to break). I am using 1.6 version and the external script is using an older version.

How can I force that only one version of jquery is loaded and it is the version I have on the page not the one loaded externally?


回答1:


This is actually pretty easy to do, using jquery.noConfict();

<script src='jquery-1.6.js'></script>
<script>
var jq16 = jQuery.noConflict();
</script>
<script src='jquery-1.4.2.js'></script>
<script>
var jq142 = jQuery.noConflict();
</script>

You can then have functions use the $() namespace as so:

 (function($) {
$('<button>Use jQuery 1.4.2</button>')
    .click(function() {
        alert('Top: ' + $(this).offset().top + '\n' +
            'jQuery: ' + $.fn.jquery);
    })
    .appendTo('body');
})(jq142);

There is a more detailed example and a few extra tips on this page.




回答2:


USE .noConflict() instead?
Ex:
I have this into my <head> inside the HTML document:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
<script type="text/javascript">
jQuery.noConflict()
jQuery16 = jQuery
</script>

And into my jQ script I call it like:

jQuery16(document).ready(function($){


$('.textarea').text(' Hello World! ');



});

Doing so I'm shure that MY script will not interfer with an external that uses some other / older version of jQuery.



来源:https://stackoverflow.com/questions/6272181/jquery-only-allow-one-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!