참조: http://api.jquery.com/jQuery.noConflict/
방법1) This technique is especially effective in conjunction with the .ready() method’s ability to alias the jQuery object, as within callback passed to .ready() we can use $ if we wish without fear of conflicts later:
<script type="text/javascript" src="other_lib.js"></script><script type="text/javascript" src="jquery.js"></script><script type="text/javascript">// <![CDATA[
jQuery.noConflict();
jQuery(document).ready(function($) {
// Code that uses jQuery's $ can follow here.
});
// Code that uses other library's $ can follow here.
// ]]></script>
예제)
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script><script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script><script type="text/javascript">// <![CDATA[
jQuery.noConflict();
jQuery(document).ready(function($) {
// Code that uses jQuery's $ can follow here.
$("div p").show();
});
// Code that uses other library's $ can follow here.
document.observe("dom:loaded", function(){
$("content").style.display = "block";
//$$('div#content').invoke('show');
});
// ]]></script>
Relinquish jQuery’s control of the ‘$’ variable.
code using $ as alias to jQuery
other code using $ as an alias to the other library, prototype.js
방법2) Reverts the $ alias and then creates and executes a function to provide the $ as a jQuery alias inside the functions scope.
jQuery.noConflict();
(function($) {
$(function() {
// more code using $ as alias to jQuery
});
})(jQuery);
// other code using $ as an alias to the other library
예제)
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script><script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script><script type="text/javascript">// <![CDATA[
jQuery.noConflict();
(function($) {
$(function() {
// more code using $() as alias to jQuery
$("div p").show();
});
})(jQuery);
// other code using $() as an alias to the other library
document.observe("dom:loaded", function(){
$("content").style.display = "block";
});
// ]]></script>
Relinquish jQuery’s control of the ‘$’ variable.
code using $ as alias to jQuery
other code using $ as an alias to the other library, prototype.js