第一类: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 areajQuery(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))

jquery 与 prototype 冲突 Using jQuery with Other Libraries相关推荐

  1. 解决jquery和prototype库冲突问题

    解决jquery和prototype库冲突问题 参考文章: (1)解决jquery和prototype库冲突问题 (2)https://www.cnblogs.com/Joanna-Yan/p/483 ...

  2. Jquery的$命名冲突

    在Jquery中,$是JQuery的别名,所有使用$的地方也都可以使用JQuery来替换,如$('#msg')等同于JQuery('#msg')的写法.然而,当我们引入多个js库后,在另外一个js库中 ...

  3. jquery源码中noConflict(防止$和jQuery的命名冲突)的实现原理

    jquery源码中noConflict(防止$和jQuery的命名冲突)的实现原理 最近在看jquery源码分析的视频教学,希望将视频中学到的知识用博客记录下来,更希望对有同样对jquery源码有困惑 ...

  4. jQuery.noConflict() 解决冲突 原理深入

    jQuery.noConflict()函数用于让出jQuery库对变量$(和变量jQuery)的控制权. 一般情况下,在jQuery库中,变量$是变量jQuery的别名,它们之间是等价的,例如jQue ...

  5. Jquery的$命名冲突详细解析

    在Jquery中,$是JQuery的别名,所有使用$的地方也都可以使用JQuery来替换,如$('#msg')等同于JQuery('#msg')的写法.然而,当我们引入多个js库后,在另外一个js库中 ...

  6. jQuery和Prototype兼容问题

    实例: Code <script type="text/javascript">     var j = jQuery.noConflict();     j(func ...

  7. ajax注解解决中文乱码,基于注解的简单MVC框架的实现,以及jquery,prototype,ajax传输乱码问题的一点解决方法...

    1:基于注解的简单MVC框架的实现 效果:1:用户只需要定义一些普通的java类来做为M层,也就是STRUTS的action类,该类里包含1到 N个控制方法,每个方法需要的form数据,由注解@Act ...

  8. 可以用jQuery代替$避免冲突

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. jquery和zepto冲突解决以及体会

    为什么80%的码农都做不了架构师?>>>    ##背景 最近公司在做一个基于移动端的项目,算是第一次公司好几个人都参与的,在前端的js插件选用上,前端人员选择了使用zepto,前端 ...

最新文章

  1. d3.js--04(enter和exit)
  2. .net Windows服务程序和安装程序制作图解
  3. 计算机精英协会考核题 —— 第三题:斐波那契数
  4. 有了这个运维方案,让IT信息化人员头疼的系统宕机再也没出现
  5. 初识推荐算法---算法背景、算法概念介绍、推荐信息选取、常用推荐算法简介
  6. 吴恩达深度学习 —— 2.4 梯度下降
  7. vue 父组件使用keep-alive和infinite-scroll导致在子组件触发父组件的infinite-scroll方法...
  8. Mysql启动找不到mysql.sock文件问题(Centos7)
  9. 图片延迟加载(lazyload)的实现原理
  10. opencv 摄像头偏色问题 自动变亮问题 解决办法
  11. 行测判断推理部分之图形推理(位置)
  12. bat文件的@echo off是什么作用?
  13. 咖啡壶的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  14. 农民抗征地住帐篷夜间起火1死3伤
  15. redhat 7中DNS 服务器配置与测试
  16. 读论文,第十一天:Flexible Strain Sensors for Wearable Hand Gesture Recognition: From Devices to Systems
  17. 摸鱼技能学习-持续更新
  18. Play框架最快上手!
  19. 歌剧小杂文3:瓦格纳-颠覆歌剧的无冕之王
  20. Heritrix 拓展Heritrix

热门文章

  1. 买铅笔(洛谷-P1909)
  2. 日语学习-多邻国-关卡1-家庭
  3. C++:unordered_map
  4. 浅谈opencl之错误码
  5. 卸载Android虚拟机里的项目(cmd)
  6. c语言加速度积分得到速度_自编微积分教材-第一章 微积分漫谈(1)
  7. python实现将一个文件夹下的文件路径写入到指定的txt文件中
  8. docker 学习笔记一(教程) 快速上手
  9. vue+element【后台案例 · 搜集 · 集锦】
  10. dedeCMS 会员:个人空间模板style修改