多くのJavaScriptライブラリは$
、jQueryと同じように、関数名または変数名として使用します。jQueryの場合、$
は単なるエイリアスであるためjQuery
、$を使用しなくてもすべての機能を利用できます。jQueryと一緒に別のJavaScriptライブラリを使用する必要がある場合は、次の呼び出しを使用して$の制御を他のライブラリに戻すことができます$.noConflict()
。
<script type="text/javascript" src="other_lib.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$.noConflict();
// Code that uses other library's $ can follow here.
</script>
この手法は、.ready()に渡されるコールバック内で、後で競合を恐れずに$を使用できるため、jQueryオブジェクトをエイリアスする.ready()メソッドの機能と組み合わせると特に効果的です。
<script type="text/javascript" src="other_lib.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$.noConflict();
jQuery(document).ready(function($) {
// Code that uses jQuery's $ can follow here.
$("div").hide();
});
// Code that uses other library's $ can follow here.
$("content").style.display = 'none';
</script>
ソース:jQuery.noConflict