Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

javascript - 我可以在同一页面上使用多个版本的jQuery吗?(Can I use multiple versions of jQuery on the same page?)

A project I'm working on requires the use of jQuery on customers' Web pages.(我正在从事的项目需要在客户的网页上使用jQuery。)

Customers will insert a chunk of code that we'll supply which includes a few <script> elements that build a widget in a <script> -created <iframe> .(客户将插入我们将提供的代码块,其中包括一些<script>元素,这些元素可在<script>创建的<iframe>中构建窗口小部件。) If they aren't already using the latest version of jQuery, this will also include (most likely) a <script> for Google's hosted version of jQuery.(如果他们尚未使用最新版本的jQuery,则还可能(很可能)包含Google托管版本的jQuery的<script> 。) The problem is that some customers may already have an older version of jQuery installed.(问题在于某些客户可能已经安装了旧版本的jQuery。) While this may work if it's at least a fairly recent version, our code does rely on some recently introduced functionality in the jQuery library, so there are bound to be instances when a customer's jQuery version is just too old.(尽管这可能至少有效,但是我们的代码确实依赖jQuery库中最近引入的某些功能,因此,在某些情况下,客户的jQuery版本太旧了。) We can't require that they upgrade to the latest version of jQuery.(我们不能要求它们升级到最新版本的jQuery。) Is there any way to load a newer version of jQuery to use only within the context of our code, that will not interfere with, or affect, any code on the customer's page?(有没有办法加载新版本的jQuery以仅在我们代码的上下文中使用,而不会干扰或影响客户页面上的任何代码?) Ideally, maybe we could check for the presence of jQuery, detect the version, and if it's too old, then somehow load the most recent version just to use for our code.(理想情况下,也许我们可以检查jQuery的存在,检测版本,如果版本太旧,则以某种方式加载最新版本以用于我们的代码。) I had the idea of loading jQuery in an <iframe> in the customer's domain that also includes our <script> , which seems like it might be feasible, but I'm hoping there's a more elegant way to do it (not to mention without the performance and complexity penalties of extra <iframe> s).(我的想法是将jQuery加载到客户域的<iframe>中,该域还包括我们的<script> ,这似乎是可行的,但我希望有一种更优雅的方法(更不用说额外的<iframe>的性能和复杂性损失)。)   ask by Bungle translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, it's doable due to jQuery's noconflict mode.(是的,由于jQuery的noconflict模式,这是可行的。)

http://blog.nemikor.com/2009/10/03/using-multiple-versions-of-jquery/(http://blog.nemikor.com/2009/10/03/using-multiple-versions-of-jquery/) <!-- load jQuery 1.1.3 --> <script type="text/javascript" src="http://example.com/jquery-1.1.3.js"></script> <script type="text/javascript"> var jQuery_1_1_3 = $.noConflict(true); </script> <!-- load jQuery 1.3.2 --> <script type="text/javascript" src="http://example.com/jquery-1.3.2.js"></script> <script type="text/javascript"> var jQuery_1_3_2 = $.noConflict(true); </script> Then, instead of $('#selector').function();(然后,代替$('#selector').function();) , you'd do jQuery_1_3_2('#selector').function();(,您可以使用jQuery_1_3_2('#selector').function();) or jQuery_1_1_3('#selector').function();(或jQuery_1_1_3('#selector').function();) .(。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...