jQuery.noConflict

jQuery.noConflict( [removeAll] ) Returns: Object

Description: Relinquish jQuery's control of the $ variable.

  • version added: 1.0 jQuery.noConflict( [removeAll] )

    removeAll A Boolean indicating whether to remove all jQuery variables from the global scope (including jQuery itself).

Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case, $ is just an alias for jQuery , so all functionality is available without using $ . If we need to use another JavaScript library alongside jQuery, we can return control of $ back to the other library with a call to $.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>

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">
  $.noConflict();
  jQuery(document).ready(function($) {
    // Code that uses jQuery's $ can follow here.
  });
  // Code that uses other library's $ can follow here.
</script>

If necessary, we can free up the jQuery name as well by passing true as an argument to the method. This is rarely necessary, and if we must do this (for example, if we need to use multiple versions of the jQuery library on the same page), we need to consider that most plug-ins rely on the presence of the jQuery variable and may not operate correctly in this situation.

Examples:

Example: Maps the original object that was referenced by $ back to $.
jQuery
.
noConflict
();


// Do something with jQuery

jQuery
(
"div p"
).
hide
();


// Do something with another library's $()

$
(
"content"
).
style
.
display 
=
 
'none'
;

Example: Reverts the $ alias and then creates and executes a function to provide the $ as a jQuery alias inside the functions scope. Inside the function the original $ object is not available. This works well for most plugins that don't rely on any other library.
jQuery
.
noConflict
();


(
function
(
$
)
 
{
 
  $
(
function
()
 
{

    
// more code using $ as alias to jQuery

  
});


})(
jQuery
);


// other code using $ as an alias to the other library

Example: You can chain the jQuery.noConflict() with the shorthand ready for a compact code.
jQuery
.
noConflict
()(
function
(){

    
// code using jQuery


});
 

// other code using $ as an alias to the other library

Example: Creates a different alias instead of jQuery to use in the rest of the script.
var
 j 
=
 jQuery
.
noConflict
();


// Do something with jQuery

j
(
"div p"
).
hide
();


// Do something with another library's $()

$
(
"content"
).
style
.
display 
=
 
'none'
;

Example: Completely move jQuery to a new namespace in another object.
var
 dom 
=
 
{};

dom
.
query 
=
 jQuery
.
noConflict
(
true
);

Result:
// Do something with the new jQuery
dom.query("div p").hide();
// Do something with another library's $()
$("content").style.display = 'none';
// Do something with another version of jQuery
jQuery("div > p").hide();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值