簡體   English   中英

如果jquery.js兩次包含在頁面中怎么辦?

[英]what if jquery.js is include into the page twice?

如果我兩次將jquery.js引入頁面( 無意或有意 ),會發生什么?

jQuery中是否有任何機制可以處理這種情況?

在AFAIK中,后一個jquery將覆蓋前一個jquery,並且如果與前一個jquery有某種動作綁定,則將其清除。

如何避免后一個覆蓋前一個?

===編輯===

我不明白為什么這個問題投了反對票。 否決票的人可以給出答案嗎?

==再次編輯== @ user568458

對了,現在是測試代碼:

<html>
<head>
</head>
<body>
fast<em id="fast"></em><br>
slow<em id="slow"></em><br>
<em id="locker"></em>
</body>
<script type="text/javascript">
function callback(type){
    document.getElementById(type).innerHTML=" loaded!";
    console.log($.foo);
    console.log($);
    console.log($.foo);
    $("#locker").html(type);
    console.log($("#locker").click);
    $("#locker").click(function(){console.log(type);});
    $.foo = "fast";
    console.log($.foo);
}
function ajax(url, type){
    var JSONP = document.createElement('script');
    JSONP.type = "text/javascript";
    JSONP.src = url+"?callback=callback"+type;
    JSONP.async = JSONP.async;
    JSONP.onload=function(){
        callback(type);
    }
    document.getElementsByTagName('head')[0].appendChild(JSONP);
}
</script>
<script>
ajax("http://foo.com/jquery.fast.js", "fast");
ajax("http://foo.com/jquery.slow.js", "slow");
</script>
</html>

它產生了結果:

undefined test:12
function (a,b){return new e.fn.init(a,b,h)} test:13
undefined test:14
function (a,c){c==null&&(c=a,a=null);return arguments.length>0?this.bind(b,a,c):this.trigger(b)} test:16 
fast test:19
undefined test:12
function (a,b){return new e.fn.init(a,b,h)} test:13
undefined test:14
function (a,c){c==null&&(c=a,a=null);return arguments.length>0?this.bind(b,a,c):this.trigger(b)} test:16 
fast

前一個(jquery.fast.js)的令牌“ $”將被后一個(jquery.slow.js)的令牌“ $”覆蓋。

有什么方法可以避免覆蓋?

我確實嘗試了以下代碼:

<html>
<head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
            $(function() {
                    $('#try').click(function() {
                            alert('ok');
                    });
            });
    </script>
    <script type="text/javascript" src="jquery.js"></script>
</head>
<body>

<button id="try">Try me</button>

</body>
</html>

沒事 點擊后,我會收到警報。 如果第二個jquery.js在按鈕之前或之后在body標簽中加載,則結果相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM