第一类:Overriding the $
-function (重写$)
However, you can override that default by calling jQuery.noConflict () at any point after jQuery and the other library have both loaded. For example:
第一种是 加入jQuery.noConflict(); 用jQuery代替$
<html> <head> <script src="prototype.js"></script> <script src="jquery.js"></script> <script> jQuery.noConflict(); // Use jQuery via jQuery(...) jQuery(document).ready(function(){ jQuery("div").hide(); }); // Use Prototype with $(...), etc. $('someid').hide(); </script> </head> <body></body> </html>
This will revert $ back to its original library. You'll still be able to use "jQuery" in the rest of your application.
Additionally, there's another option. If you want to make sure that jQuery won't conflict with another library - but you want the benefit of a short name, you could do something like this:
第二种是 赋值jQuery.noConflict()于另一变量; 用这一变量代替$
<html> <head> <script src="prototype.js"></script> <script src="jquery.js"></script> <script> var $j = jQuery.noConflict(); // Use jQuery via $j(...) $j(document).ready(function(){ $j("div").hide(); }); // Use Prototype with $(...), etc. $('someid').hide(); </script> </head> <body></body> </html>
You can define your own alternate names (e.g. jq, $J, awesomeQuery - anything you want).
Finally, if you don't want to define another alternative to the jQuery name (you really like to use $ and don't care about using another library's $ method), then there's still another solution for you. This is most frequently used in the case where you still want the benefits of really short jQuery code, but don't want to cause conflicts with other libraries.
第三种是使用jQuery(document).ready(function($){})
<html> <head> <script src="prototype.js"></script> <script src="jquery.js"></script> <script> jQuery.noConflict(); // Put all your code in your document ready area jQuery(document).ready(function($){ // Do jQuery stuff using $ $("div").hide(); }); // Use Prototype with $(...), etc. $('someid').hide(); </script> </head> <body></body> </html>
This is probably the ideal solution for most of your code, considering that there'll be less code that you'll have to change, in order to achieve complete compatibility.
第一类:Including jQuery
before Other Libraries (把jquery写在其他库的前面,这是不需要加入jQuery.noConflict())
If you include jQuery before other libraries, you may use "jQuery" when you do some work with jQuery, and the "$" is also the shortcut for the other library. There is no need for overriding the $
-function by calling "jQuery.noConflict()".
<html> <head> <script src="jquery.js"></script> <script src="prototype.js"></script> <script> // Use jQuery via jQuery(...) jQuery(document).ready(function(){ jQuery("div").hide(); }); // Use Prototype with $(...), etc. $('someid').hide(); </script> </head> <body></body> </html>
Referencing Magic - Shortcuts for jQuery
If you don't like typing the full "jQuery
" all the time, there are some alternative shortcuts:
- Reassign jQuery to another shortcut
var $j = jQuery;
- (This might be the best approach if you wish to use different libraries)
- Use the following technique, which allows you to use
$
inside of a block of code without permanently overwriting $:(function($) { /* some code that uses $ */ })(jQuery)
-
Note: If you use this technique, you will not be able to use Prototype methods inside this capsuled function that expect
$
to be Prototype's$
, so you're making a choice to use only jQuery in that block.
- Use the argument to the DOM ready event :
jQuery(function($) { /* some code that uses $ */ });
- Note: Again, inside that block you can't use Prototype methods
我的用法:
1. 编辑 jquery.js 在最后添加
// ... jquery code ... A.jQuery=A.$=c})(window); .... var _gls = jQuery.noConflict(true);
2. 在 javascript 页面添加:
(function($){$(function() { // jquery code // eg: $('.class').click(function(){}); })}(_gls))
相关推荐
在给定的标题"JavaScript libraries--->jQuery, Prototype, Mootools, YUI, Extjs, Dojo"中,提到了五种著名的JavaScript库:jQuery、Prototype、Mootools、YUI和ExtJS。这些库都有各自的特性和优势,下面将详细介绍...
- **Using JQuery with Other Libraries:** JQuery is designed to play well with other JavaScript libraries and frameworks. By using the `.noConflict()` method, developers can integrate JQuery ...
- **Using jQuery with Other Libraries**:探讨如何将 JQuery 与其他 JavaScript 库一起使用,以充分利用各自的优势。 #### 二、实践应用 除了理论知识,《JQuery教程全集》还包含了丰富的案例,帮助读者将所学...
4. **与其它库的兼容(Compatibility with Other Libraries)**:jQuery提供`.noConflict()`方法,解决与其他JavaScript库的命名冲突问题。 四、jQuery学习资源 1. **官方文档(Official Documentation)**:...
- **Using jQuery with Other Libraries**: jQuery integrates seamlessly with other JavaScript libraries, making it possible to combine the strengths of multiple tools. **4. Selecting Elements for ...
4. **避免全局作用域污染**:通过`jQuery.noConflict()`方法,可以防止jQuery与其它使用$符号的库发生冲突。 5. **学习和掌握API**:深入理解jQuery API将有助于编写更高效、简洁的代码,提升开发效率。 综上所述...
2. 配置jQuery:在DWCS5中,打开“Edit > Preferences > JavaScript”,在"Code Libraries"部分点击"+"按钮,然后浏览到jQuery的js文件(通常为jquery.min.js),添加并保存。 3. 使用jQuery:现在,你可以在DW中...
“Responsive Web Design with jQuery” is a practical book focused on saving your development time using the useful jQuery plugins made by the frontend community. Follow the chapters, and learn to ...
Practical jQuery is your step-by-step guide to using jQuery in the real world, taking you from downloading jQuery all the way to extending it by writing your own plug-ins and testing the DOM using ...
At the end of the book, you’ll put everything together to build a fully functional social networking site suitable for both desktop and mobile browsers, using the AMPPS or any other popular ...
3. 第三方平台:一些第三方网站,如cdnjs(https://cdnjs.com/libraries/jquery)或jsDelivr(https://www.jsdelivr.com/package/npm/jquery),提供了CDN加速的jQuery及其插件,可以直接在HTML中引用,无需下载。...
《Getting Started with LLVM Core Libraries》是一本关于LLVM(Low Level Virtual Machine)核心库入门的书籍。LLVM是一套广泛使用的开源基础架构,专为编译器开发设计。本书详细阐述了LLVM核心库的安装过程、使用...
5. 点击“添加”按钮,浏览到你的jQuery库文件位置(通常在DW的"Libraries"文件夹内),选择jQuery的主js文件(如`jquery.js`或`jquery.min.js`)。 6. 确认添加后,关闭首选项设置窗口。 现在,当你在DW中编写...