Jquery-noconflict

提供:Dev Guides
移動先:案内検索

jQuery-noConflict()

多くのJavaScriptライブラリは、jQueryと同様に、関数名または変数名として$を使用します。 jQueryの場合、$はjQueryの単なるエイリアスであるため、$を使用せずにすべての機能を使用できます。

  • $。noConflict()*メソッドを実行して、$変数の制御を最初に実装したライブラリに戻します。 これは、jQueryが他のライブラリの$オブジェクトと競合しないことを確認するのに役立ちます。

これは、競合を回避する簡単な方法です-

//Import other Library
//Import jQuery Library
$.noConflict();
//Code that uses other library's $ can follow here.

この手法は、.ready()メソッド内でjQueryオブジェクトをエイリアスする機能と併用すると特に効果的です。.ready()内で、後で競合を恐れずに$を使用できる場合-

//Import other library
//Import jQuery
$.noConflict();
jQuery(document).ready(function($) {
  //Code that uses jQuery's $ can follow here.
});
//Code that uses other library's $ can follow here.