2

私はウェブキットのみを使用しています。プロトタイプが既に読み込まれているページに jQuery を挿入する必要があります。このコードを使用してjQueryをロードしています。(コンソールで試すことができます)

var s = document.createElement('script');
s.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.js');
s.setAttribute('type', 'text/javascript');
document.getElementsByTagName('head')[0].appendChild(s);

上記のコードだけでエラーが発生します。

ロード時に noConflict() を使用するにはどうすればよいですか。jquery スクリプトを挿入した後に次のコードを挿入すると、まだエラーが発生します。

$(document).ready(function() {
  jQuery.noConflict();
  // my thing here
});

これもエラーをスローします。

jQuery.noConflict();
$(document).ready(function() {
  // my thing here
});
4

4 に答える 4

3

編集:別のスクリプトからスクリプトをロードしているjQueryため、スクリプトのロード イベントへのコールバックで実行する必要があるコードを配置する必要があります。

var s = document.createElement('script');
s.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.js');
s.setAttribute('type', 'text/javascript');
document.getElementsByTagName('head')[0].appendChild(s);

   // Place your code in an onload handler for the jQuery you're loading
s.onload = function() {

    jQuery.noConflict(); // release jQuery's hold on "$"

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

      alert( $.fn.jquery );
   });
};

もう 1 つの解決策は、jQuery をロードするこの方法を使用しないことです。要素をハードコードするだけ<script>で、コードは期待される同期方法で実行されます。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.js" type="text/javascript"></script>

<script type="text/javascript">
    jQuery.noConflict(); // release jQuery's hold on "$"

      // do this with ready() -------v------ and the "$" will be available inside
    jQuery(document).ready(function( $ ) {

      // $ is safe for jQuery inside this .ready() callback
      alert( $.fn.jquery );
    });
</script>

元の答え:

これを行う:

var s = document.createElement('script');
s.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.js');
s.setAttribute('type', 'text/javascript');
document.getElementsByTagName('head')[0].appendChild(s);


jQuery.noConflict(); // release jQuery's hold on "$"

  // do this with ready() -------v------ and the "$" will be available inside
jQuery(document).ready(function( $ ) {

  // $ is safe for jQuery inside this .ready() callback
  alert( $.fn.jquery );
});
于 2011-06-23T22:25:07.267 に答える
2

試す

var $j = jQuery.noConflict();
$j(document).ready(function() {
  // my thing here
});

その後、任意の jquery $ に $j を使用できます

于 2011-06-23T22:09:40.560 に答える
1

ここでは、$最初に を使用してから を使用します。問題は、no conflict を設定する前にjQuery.noConflict()(誤って) jQuery であると想定していたことです。$

$(document).ready(function() {
  jQuery.noConflict();
  // my thing here
});

ここでは、反対のことをしました。最初に競合のないビットを実行しましたが、それから jQuery へのアクセスに使用し続けましたが、これは (呼び出し$の直接の結果として) 機能しなくなります。noConflict()

jQuery.noConflict();
$(document).ready(function() {
  // my thing here
});

2 つの努力を組み合わせると、次のようになります。また、関数内でjQuery 参照として引き続き使用できる$ように、.ready行にa を追加しました。ready$

jQuery.noConflict(); // stops $ being associated with jQuery
jQuery(document).ready(function($) { // use long-hand jQuery object reference to call `ready()`
  // my thing here
});
于 2011-06-23T22:25:40.840 に答える
1

$ は、jQuery (およびプロトタイプ) のエイリアス/ショートカットです。NoConflict は基本的に $ ショートカットの制御を解放するため、呼び出されると、他のライブラリがそれを制御できるようになります。これを試して:

jQuery(document).ready(function() {
  // my thing here
});
于 2011-06-23T22:08:00.997 に答える