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.
jquery-overview.htm
广告
© . All rights reserved.