jquery와 prototype 충돌 Using jQuery with Other Libraries

클래스 1: 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.
 
 
 
첫 번째 종류: IncludingjQuerybefore 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. 자바스크립트 페이지에 추가하기
    (function($){$(function()
    {
        // jquery code
        // eg: $('.class').click(function(){});
    
    })}(_gls))

     
     
     
     

    좋은 웹페이지 즐겨찾기