Using jQuery with Other Libraries:一直没有怎么看JQuery更新后的Documentation,原来为兼用Prototype,他们给出了好几个解决方法。

吕的部落格唯女子与小人为难养也近之则不孙远之则怨

Using jQuery with Other Libraries

 General

The jquery library, and virtually all of its plugins are constrained within the jquery namespace. As a general rule, "global" objects are stored inside the jquery namespace as well, so you shouldn't get a clash between jquery and any other library (like prototype, MooTools, or YUI).

That said, there is one caveat: By default, jquery uses "$" as a shortcut for "jquery"
[edit]
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:

 <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').style.display = 'none';
   </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:

 <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').style.display = 'none';
   </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.

 <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').style.display = 'none';
   </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.
[edit]
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
          o var $j = jquery;
          o (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 $:
          o (function($) { // some code that uses $ })(jquery)
          o 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:
          o jquery(function($) { // some code that uses $ });
          o Note: Again, inside that block you can't use prototype methods
所属分类:技术手册标签:jquery UsingjQuerywithOtherLibrarie 吕 @ 2007年2月13日8点21分 AM 编辑 Using jQuery with Other Libraries阅读(1973) 评论(0)

相关文章

评论

还没有评论
未开放评论

谷歌中Using jQuery with Other Libraries相关文章

数据加载中,请稍候……