jQuery 结合其他库一起使用,为了解决名字空间的冲突,以下是三种解决办法:

第一种方法是直接调用 jQuery.noConflict():

<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>

第二种方法是赋值给一个简短的名字:

<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>

第三种方法,通过传递$给ready函数参数作为参数,就可以在ready的参数funcion中使

用$:

<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>

相关文章:

  • 2022-12-23
  • 2021-06-13
  • 2021-07-15
  • 2022-02-24
  • 2021-07-16
  • 2021-11-19
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-14
  • 2021-08-16
  • 2021-10-18
  • 2021-09-18
  • 2021-09-28
  • 2021-10-07
  • 2021-10-16
相关资源
相似解决方案