在同一页面中使用JQuery和Prototype [英] Using JQuery and Prototype in the same page

查看:125
本文介绍了在同一页面中使用JQuery和Prototype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的几个页面同时使用JQuery和Protoype。由于我升级到JQuery的1.3版本,这似乎导致了问题,因为两个库都定义了一个名为'$'的函数。

Several of my pages use both JQuery and Protoype. Since I upgraded to version 1.3 of JQuery this appears to be causing problems, because both libraries define a function named '$'.

JQuery提供了一个函数noConflict(),它放弃了控制$可能正在使用它的其他库。因此,似乎我需要浏览所有看起来像这样的页面:

JQuery provides a function noConflict() which relinquishes control of $ to other libraries that may be using it. So it seems like I need to go through all my pages that look like this:

<head>      
    <script type="text/javascript" src="/obp/js/prototype.js"></script>
    <script type="text/javascript" src="/obp/js/jquery.js"></script>
</head>

并将其更改为:

<head>      
    <script type="text/javascript" src="/obp/js/prototype.js"></script>
    <script type="text/javascript" src="/obp/js/jquery.js"></script>
    <script type="text/javascript">
        jQuery.noConflict();
        var $j = jQuery;
    </script>
</head>

然后我应该能够使用'$'代替Prototype和'$ j'(或'jQuery' ')用于JQuery。我并不完全乐意在每个相关页面中复制这两行代码,并期望在某些时候有人可能忘记将它们添加到新页面。我希望能够执行以下操作

I should then be able to use '$' for Prototype and '$j' (or 'jQuery') for JQuery. I'm not entirely happy about duplicating these 2 lines of code in every relevant page, and expect that at some point somebody is likely to forget to add them to a new page. I'd prefer to be able to do the following


  • 创建文件j query-noconflict.js 其中包含 jquery.js 以及上面显示的2行代码

  • 导入 jquery-noconflict.js (而不是 jquery.js )在我的所有JSP / HTML页面中

  • Create a file jquery-noconflict.js which "includes" jquery.js and the 2 lines of code shown above
  • Import jquery-noconflict.js (instead of jquery.js) in all my JSP/HTML pages

但是,我不确定是否可以按照我描述的方式将一个JS文件包含在另一个JS文件中?当然另一种解决方案就是直接将上面两行代码添加到jquery.js中,但如果我这样做,我每次升级JQuery时都需要记住这样做。

However, I'm not sure if it's possible to include one JS file in another, in the manner I've described? Of course an alternate solution is simply to add the 2 lines of code above to jquery.js directly, but if I do that I'll need to remember to do it every time I upgrade JQuery.

推荐答案

目前你可以这样做:

<head>          
    <script type="text/javascript" src="/obp/js/prototype.js"></script>
    <script type="text/javascript" src="/obp/js/jquery.js"></script>
    <script type="text/javascript">
        var $j = jQuery.noConflict();
    </script>
</head>

然后,使用jQuery作为 $ j()和Prototype的 $()

Then, use jQuery as $j() and Prototype's $().

这篇关于在同一页面中使用JQuery和Prototype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆