结合jQuery与Prototype在你的项目里:前阵子遇到JQuery和Prototype有冲突的事,原来JQuery网站上早就说了这个问题,原文 呵呵,这下项目里又可以同时使用这个两个框架了.

吕的部落格唯女子与小人为难养也近之则不孙远之则怨

结合jQuery与Prototype在你的项目里

前阵子遇到jqueryprototype有冲突的事,原来jquery网站上早就说了这个问题,原文
呵呵,这下项目里又可以同时使用这个两个框架了.
注意,这并不代表项目可以随意继承,因为jquery改成了$()方法,所以使用Prototypre的$()可能会出错

prototypeAndjquery

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.

所属分类:技术手册标签:JQueryAjaxPrototype 结合jQueryPrototype在你项目 吕 @ 2006年11月6日9点09分 AM 编辑 结合jQuery与Prototype在你的项目里阅读(3697) 评论(2)

相关文章

评论

  • 柠檬园主 @ 2007-1-7 15:11:00 回复1#RE:结合jQuery与Prototype在你的项目里

    那请问,如果我把jquery放在prototype之后了,那我之前用的prototype的$()方法还需要更改吗?因为我现在的东西中也是用了大量的prototype的东西.

    但jquery的事件绑定确实是个好东西,能很好的实现结构分离,有点想往了.

  • 吕 @ 2007-1-9 7:59:56 回复2#RE:结合jQuery与Prototype在你的项目里

    只有改自己的,将$('ID')改成$('#ID')即可.
    另外Jquery可以通过Class定位,如果你也是用ASP.NET,那么使用Jquery就不像prototype那么麻烦了

注:部分评论不可见.
标题(请在评论前按F5或直接注册会员,否则您的评论可能不会成功)
姓名*
主页或邮箱
验证码*如无法识别验证码请双击(如无法识别请双击图片更换)
评论内容
     

谷歌中结合jQuery与Prototype在你的项目里相关文章

数据加载中,请稍候……