在同一页面上使用不同版本的jQuery

问题描述:

我的服务用户在我提供的页面中包含一个JS.我正在托管其中包括的脚本.我的脚本对其内容进行了一些操作.我讨厌编写自己的DOM操纵器/选择器,并且如果可以使用jQuery,则浪费大量的工作来完成可以用1行代码完成的工作.

Users of my service includes a JS in their pages that I provide. I am hosting the script they are including. My script does some manipulation on their content. I am sick of writing my own DOM manipulators/selectors and wasting hours for jobs that can be done with 1 line of code if I can use jQuery.

我的服务的某些用户已经在其页面上使用jQuery(或Prototype等),因此,如果我包含jQuery,将会发生冲突.因为会有版本差异,所以我不想使用它们的jQuery选择器,以防jQuery存在.

Some of the users of my service already uses jQuery (or Prototype etc.) on their pages, so if I include jQuery there will be a conflict. Because there will be version differences, I don't want to use their jQuery selectors, methods either in case jQuery exists.

请记住,我无法控制他们的页面,如何包含jQuery并避免冲突?

Keeping in mind that I have no control over their pages, how can I include jQuery and avoid conflict?

大多数情况下,我需要选择器,所以有一天我偶然发现了它:

Mostly I needed selectors, so the other day I stumbled onto this: Sizzle, A pure-JavaScript CSS selector engine designed to be easily dropped in to a host library.

它可用于避免与任何其他Sizzle实例冲突.我很高兴找到了这个解决方案.

It can be used to avoid conflicts with any other Sizzle instances. I am very happy that I found this solution.