问题
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