jQuery和Prototype的兼容性和冲突的五种解决方法
来源:互联网 发布:美妆po主 知乎 编辑:程序博客网 时间:2024/05/29 11:42
页面中的方法既用到jQuery同时又用到prototype,最后出现prototype方法执行不了,解决方法采用的第二种情况中的方法1和第一种情况的方法1 jQuery.noConflict();。
第一种情况:先加载Prototype,再加载jQuery
方法一:jQuery 库和它的所有插件都是在jQuery名字空间内的,包括全局变量也是保存在jQuery 名字空间内的。
使用jQuery.noConflict();主要作用是在任何时候,只要在jQuery加载后就可以调用,将$符号的使用权返回给其它的js库,jQuery在创建它自己的名字空间时就将其它库的$保存在自己的一个变量当中。
- <html>
- <head>
- <script src="prototype.js"></script>
- <script src="jquery.js"></script>
- <script type="text/javascript" >
- //各个js库之间的主要冲突在于$的冲突,这个方法是用来处理这个问题的
- jQuery.noConflict();
- //原本使用jQuery代码部分的$ 用jQuery替代
- jQuery(document).ready(function (){
- jQuery("div").hide();
- });
- // Use Prototype with $(...), etc.
- $('proto').hide();
- </script>
- </head>
- <body></body>
- </html>
方法二:如果你仍然想使用类似于$这样比较简短的字符,你可以将jQuery.noConflict()的返回值赋值给某个变量。这个变量就是jQuery的新缩写了,当然你可以使用$以外的任意字符串,比如:
- <html>
- <head>
- <script src="prototype.js"></script>
- <script src="jquery.js"></script>
- <script type="text/javascript" >
- //$j就相当于jQuery,名称你可以自主定义
- var $j = jQuery.noConflict();
- // Use jQuery via $j(...)
- $j(document).ready(function (){
- $j("div").hide();
- });
- // Use Prototype with $(...), etc.
- $('proto').hide();
- </script>
- </head>
- <body></body>
- </html>
方法三:如果你还是想使用$,而不想使用别的字符,也是可以的。而且通常程序员都比较喜欢这样做,因为这样做写好的代码几乎都不用替换原来的$符号。那就是利用名字空间的概念就所有的jQuery代码封装在document的ready事件名字空间范围内,如:jQuery(document).ready(这里填入jQuery代码)
- <html>
- <head>
- <script src="prototype.js"></script>
- <script src="jquery.js"></script>
- <script type="text/javascript" >
- jQuery.noConflict();
- // Put all your code in your document ready area
- jQuery(document).ready(function ($){
- // 这样你可以在这个范围内随意使用$而不用担心冲突
- $("div" ).hide();
- });
- // Use Prototype with $(...), etc.
- $('proto' ).hide();
- </script>
- </head>
- <body></body>
- </html>
第二种情况:先加载jQuery,再加载Prototype
按照这样的顺序加载,就不存在其它js库的$符号被jQuery占用的问题。所以对其它的js库的代码可以不作任何修改,照常使用$,而对 jQuery可以使用jQuery来替代$.如:
- <html>
- <head>
- <script src="jquery.js"></script>
- <script src="prototype.js"></script>
- <script type="text/javascript" >
- // 使用 jQuery 用 jQuery(...)
- jQuery(document).ready(function (){
- jQuery("div" ).hide();
- });
- // 使用 Prototype 时,用 $(...),
- $('someid' ).hide();
- </script>
- </head>
- <body></body>
- </html>
或者你不想写jQuery这么长的字符,你可以通过另外一种方法:
- var $j = jQuery;
转自:http://blog.csdn.net/aoyoo111/article/details/7364349
- jQuery和Prototype的兼容性和冲突的五种解决方法
- jQuery和Prototype的兼容性和冲突的五种解决方法
- jQuery和Prototype的兼容性和冲突的五种解决方法
- jQuery和Prototype的兼容性和冲突的五种解决方法
- jQuery和Prototype的兼容性和冲突的解决方法
- prototype和jquery冲突 解决方法
- jQuery和prototype冲突的解决办法:
- prototype 和jquery 冲突的解决办法
- jquery 和prototype 同时用的冲突问题
- magento 添加jquery已经解决和prototype的冲突
- magento 添加jquery已经解决和prototype的冲突
- magento引入jquery库和prototype库冲突的解决办法
- jQuery和JS文件冲突的解决方法
- UpdatePanel和jquery冲突的解决方法
- jquery和prototype冲突解决
- jquery和prototype冲突解决
- jquery和prototype冲突问题
- jquery和prototype冲突解决
- 数据库的一些基础知识
- charles代理的使用
- The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Cha
- Mysql操作常用命令
- php读取excel文件的日期时间问题
- jQuery和Prototype的兼容性和冲突的五种解决方法
- linux更改文件拥有者
- 关于VTK最新版6.3+vs2013+CMake3.3配置到测试流程
- 第一篇文章
- Java 中7种线程池详解+示例代码
- ahjesus Axure RP 7.0注册码
- 为什么要使用抽象类 接口编程以及有什么好处
- Hardcoded string XXX, should use @string resource警告
- spring+springMVC+mybatis 所需jar包