2011-10-25 7 views
-1

私は小さなページがあります。2つのバージョンのjQuery - > 2ドキュメント。どうして?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
    <title></title> 
    <script type="text/javascript" src="jquery-1.4.2.js"></script> 
    <script type="text/javascript" src="temp.js"></script> 
</head> 
<body> 
<p>foo</p> 
<p>bar</p> 
</body> 
</html> 

を、私はjQueryの2つの異なるバージョンをロードしようとしています:

// temp.js 
jQueryScriptOutputted = false; 
initJQuery = function() { 

    //if the jQuery object isn't available 
    if (typeof(myjQuery) == 'undefined') { 

    if (!jQueryScriptOutputted) { 
     //only output the script once.. 
     jQueryScriptOutputted = true; 

     //output the script (load it from google api) 
     document.write("<script type=\"text/javascript\" src=\"jquery-1.6.4.js\"></script>"); 
     document.write("<script type=\"text/javascript\">var myjQuery = $.noConflict(true);</script>"); 
    } 
    setTimeout("initJQuery()", 50); 
    } else { 
    myjQuery(function() { 
     // Check jQuery versions 
     console.log('myjQuery version = ' + myjQuery().jquery); 
     console.log('$ version = ' + $().jquery); 
     console.log('jQuery version = ' + jQuery().jquery); 

     // Get the data of the actual poll 
     document.write("Where is foo and bar?!?"); 
    }); 
    } 

} 
initJQuery(); 

をしかし、この2つの異なるドキュメントをロードしているようです。つまり、ページを開くと段落が失われます。どうして?!?

+6

最初の質問:なぜあなたが最初の場所でこれをやろうとしていますか? – Blazemonger

答えて

1

ページがロードされた後にdocument.writeを呼び出すと、ページ全体がdocument.writeパラメータで上書きされます。マークアップを変更するには、$().appendまたは$().htmlのようなものを使用することを検討してください。

すなわち

myjQuery(function() { 
     $('body').append("<p>Where is foo and bar?!?</p>"); 
    }); 
0

1つのバージョンのみを読み込む必要があります。 つまり、1つのjqueryライブラリのみがページにインストールされています。

関連する問題