关于prototype与jquery 的一起协作问题

本文介绍如何在同一项目中同时使用Prototype和jQuery库,并避免因两者都定义了$符号而产生的冲突。通过调整加载顺序和重命名jQuery的$函数,可以确保两库共存并正常工作。

在项目中引用第三方的webEdit,由于此组件用到了prototype, 而我们对于ajax的处理是用jquery Lib ,主要是基于jquery 优美的框架哲学,但在使用的过程进导致webedit不能使用,由于prototype与jquery都有$ ,应该是由此引起,因为以此找到一个篇关于此的解决方案

记录如下

PrototypeAndjquery

 

Back

jquery gets a lot of its inspiration from the power behind the Prototype library. This is immediately noticeable with jquery's use of the $() function, inspired by the Prototype function of the same name. However, there are some things that should be known about the Prototype and jquery interact, and how the $() behaves differently.

Using Prototype and jquery Together

只要先引入prototype再引入 jquery,个人感觉就是作者自己说的,因为是Prototype给他灵感,所以重成了Prototype的部分内容,因此先 jquery必将出现我们所不知道的错误.

To include both Javascript libraries, and have them work in unison, you will need to first include Prototype, then jquery. For example:

  <script src="prototype.js"></script>
  <script src="http://jquery.com/src/latest/"></script>

Loading jquery first, then Prototype, will cause your code to break - as a reminder, jquery throws an exception saying: "You are overwriting jquery, please include jquery last." (If you see this error, that's what it means)

Differences in $()

A side-by-side comparison of how the $() function works *ONLY WHEN PROTOTYPE IS USED* would be best to explain the differences. If you're not using Prototype, please refer to the documentation, instead.

  $("pre")

Prototype: Looks for the element with an ID of pre, if found, returns it, otherwise returns null.

jquery: Looks for all elements with the Tag name of pre.

  • If none are found: It then looks for an element with an ID of pre, if one is found - it returns that element, if not, it returns a jquery object, with an empty set of matched elements.
  • If elements are found: jquery returns a jquery object, containing the all matched pre elements.
  $(DOMElement)

Prototype: Returns the DOMElement.

jquery: Attaches all of the jquery object methods to the DOMElement, then returns it. The result should still be usable by Prototype and jquery. Note: See the bottom of the page for more information on this.

What to do about $()?

Because the behavior of Prototype and jquery is different, when it comes to the $() function, it is recommended that you do one of two things:

Un-ambiguous Selectors

Always be explicit when you search by a single ID. For example, use this:

  $("#pre")

and not this, which is ambiguous:

  $("pre")

Doing that will solve any problems straight away.

Prototype Short-hand

If you want to continuing using the Prototype short-hand, you must keep one rule in mind: Never name any of your IDs the same as a DOM Element type, otherwise you will have strange results. For example:

  $("pre")

would work, if there were no pre elements in the page, but once one was added, your code would break. A better example is:

  $("body")

which will always break (since the body element is required).

In a nutshell: Either use smart un-ambiguous !IDs, or don't name your !IDs the same as element names.

Wrapping of DOM Elements

In order to support both Prototype and jquery users at the same time, returned DOM elements have additional jquery functions attached to them. It should be noted, however, that just because the original DOM Element is being returned, its original functions and properties should not be accessed directly, for example:

When using both Prototype and jquery $("wrap") will return a modified DOM Element, so if you were inclined to do:

  $("wrap").style.display = 'none';

That would work, but only when using Prototype. If you then, later, stopped using Prototype, that code would break. To be safe, you should only use jquery-sanctioned functions and terminology, for example:

  $("#wrap").hide();

would be the proper way of doing the above - it will always work, even if you are (or aren't) using Prototype.

Using Prototype and jquery Together (other solution)

If you need use jquery and also Prototype + Scriptaculous + ... , you need rename jquery $ function. For example:

 <script src="http://jquery.com/src/latest/"></script>
 <script type="text/javascript">
    JQ = $;  //rename $ function
 </script>
 <script src="prototype.js"></script>

Then you can access to jquery function with JQ and for access to Prototype $ function use the normal name. For example:

 <script type="text/javascript">
  JQ(document).ready(function(){
   JQ("#test_jquery").html("this is jquery");
   $("test_prototype").innerHTML="this is Prototype";
  });
 </script>

NOTE: Be carefull with jquery plugins, you will need rename all $ references to JQ or other name.

代码下载地址: https://pan.quark.cn/s/b4a8e0160cfc 齿轮轴系零件在机械设备中扮演着至关重要的角色,它们负责实现动力传输、调整运动形态以及承受工作载荷等核心功能。 在机械工程的设计实践中,齿轮和轴系的设计是一项关键的技术任务,其内容涵盖了材料选用、构造规划、承载能力分析等多个技术层面。 下面将系统性地介绍《齿轮及轴系零件结构设计指导书》中的核心知识点。 一、齿轮设计1. 齿轮种类:依据齿廓轮廓的不同,齿轮可划分为直齿齿轮、斜齿轮以及人字齿轮等类别,各类齿轮均具有特定的性能特点适用工况,能够满足多样化的工作环境载荷需求。 2. 齿轮规格参数:模数大小、压力角数值、齿数数量、分度圆尺寸等是齿轮设计的基础数据,这些参数直接决定了齿轮的物理尺寸运行性能。 3. 齿轮材质选用:齿轮材料的确定需综合评估其耐磨损性能、硬度水平以及韧性表现,常用的材料包括铸铁、钢材、铝合金等。 4. 齿轮强度验证:需进行齿面接触应力分析齿根弯曲应力分析,以确保齿轮在实际运行过程中不会出现过度磨损或结构破坏。 5. 齿轮加工工艺:涉及切削加工、滚齿加工、剃齿加工、淬火处理等工艺流程,工艺方案的选择将直接影响齿轮的加工精度使用寿命。 二、轴设计1. 轴的分类方式:依据轴在机械装置中的功能定位受力特点,可将轴划分为心轴、转轴以及传动轴等类型。 2. 轴的材料选择:通常采用钢材作为轴的材料,例如碳素结构钢或合金结构钢,特殊需求时可选用不锈钢材料或轻质合金材料。 3. 轴的构造规划:需详细考虑轴的轴向长度、截面直径、键槽布置、轴承安装位置等要素,以满足轴的强度要求、刚度要求以及稳定性要求。 4. 轴的强度验证:需进行轴的扭转强度分析弯曲强度分析,以防止轴在运行过程中发生塑性变形...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值