简体   繁体   English

检查jQuery版本并加载更新版本

[英]Check jQuery version and load a newer version

I have various web pages that have varying versions of jQuery included on them ranging from 1.4.2 upwards (yes, I know, it's a mess). 我有各种各样的网页,其中包含不同版本的jQuery,范围从1.4.2以上(是的,我知道,这是一团糟)。 I want to use the jQuery validation plugin on these pages but it requires jQuery 1.6.4+. 我想在这些页面上使用jQuery验证插件,但它需要jQuery 1.6.4+。

I know I can check the version of jQuery loaded and then load in a newer version if necessary but I'm unsure on how to reference them individually so the pre-existing JavaScript/jQuery code that's on the site can still use $ (as I know this works quite happily and I don't want to break it) and then the new plugin can use something else? 我知道我可以检查加载的jQuery的版本,然后在必要时加载一个更新的版本,但我不确定如何单独引用它们,所以网站上预先存在的JavaScript / jQuery代码仍然可以使用$(因为我知道这非常愉快,我不想打破它然后新的插件可以使用其他东西吗?

Any help or suggestions on a better way to do this would be greatly appreciated. 任何有关更好的方法的帮助或建议将不胜感激。 Thanks. 谢谢。

Bad idea to do what you are doing. 做你正在做的事情的坏主意。

If you really insist on having different jquery versions, you can always write some sort of a script manager. 如果你真的坚持使用不同的jquery版本,你总是可以写一些脚本管理器。 Basically you specify in your page's "config" what jquery versions are required on this page and the manager will load the appropriate one. 基本上,您在页面的“config”中指定了此页面上需要的jquery版本,并且管理器将加载适当的版本。

Something like: 就像是:

// each page content before everything else
Manager.reguire("plugin 1", "1.4.7");
Manager.require("plugin 2", "1.4.4");


// Main layout <head section>
Manager.LoadRequiredVersions();

And the manager would just request the files for you, but don't forget that you might have to do some clever no-conflict stuff with jQuery if you want more than one on a single page. 管理员只会为你请求这些文件,但不要忘记,如果你想在一个页面上使用多个文件,你可能不得不用jQuery做一些聪明的无冲突的东西。 https://stackoverflow.com/a/1566644/486780 https://stackoverflow.com/a/1566644/486780

If it's one version per page then a manager would be the easiest option. 如果它是每页一个版本,那么经理将是最简单的选择。

  • original version of jquery loads (Version1) 原始版本的jquery加载(Version1)
    • $ and jQuery belong to version1 $和jQuery属于version1
  • second version of jquery loads (Verion2) 第二版jquery加载(Verion2)
    • now $ and jQuery belong to Version2, and _$ and _jQuery that belongs to Version1 现在$和jQuery属于Version2,_ $和_jQuery属于Version1
  • assign the Version2 version to a var ($v2 = jQuery.noConflict(true);) 将Version2版本分配给var($ v2 = jQuery.noConflict(true);)
    • now $ and jQuery belong to Version1, _$ and _jQuery are probably null, and $v2 is Version2 现在$和jQuery属于Version1,_ $和_jQuery可能为null,$ v2为Version2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM