jQuery和prototype.js发生冲突,如何将jQuery保留为$? [英] jQuery and prototype.js conflict, how to keep jQuery as $?

查看:70
本文介绍了jQuery和prototype.js发生冲突,如何将jQuery保留为$?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在一个同时使用jQuery和prototype.js的网站上工作,但是它们之间存在冲突.

So I'm working on a website that uses both jQuery and prototype.js, however their conflicting.

我进行了相当多的研究,发现人们解决此问题的唯一方法是使用

I've researched a fair bit and found that the only way people fix this issue is by using

<script>
 jQuery.noConflict();

 // Use jQuery via jQuery(...)
 jQuery(document).ready(function(){
   jQuery(\"div\").hide();
 });

 // Use Prototype with $(...), etc.
 $('someid').hide();

但是我不想将$更改为jQuery,因为这意味着我必须编辑一些预先编写的代码.有没有一种方法可以阻止它们发生冲突,并将jQuery保留为$?

However I don't want to change $ to jQuery as that means I'd have to edit a fair bit of pre written code. Is there a way to stop them conflicting and leave jQuery as $?

推荐答案

代替准备文档,您可以创建一个闭包(在body的末尾),如:

Instead of using document ready you can create a closure (at the end of the body) like:

(function($) {
  //jQuery stuff
  $('.elem') // $ refers to jQuery
})(jQuery);

如果我正确理解了您的问题

If I understand your question correctly

这篇关于jQuery和prototype.js发生冲突,如何将jQuery保留为$?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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