jQuery 与 ASP.NET Server Control 的版本冲突解决
我正在开发一个 ASP.NET 服务器控件,它使用 jQuery 来实现某些客户端逻辑。我已将 jQuery 文件作为资源嵌入到控件中。
我不想将使用控件的应用程序限制为该特定版本的 jQuery,并且我想继续使用我嵌入的 jQuery 版本。
我知道 noconflict 方法,但我看到的问题是我无法控制页面上脚本标签的顺序。
如果用户的 jQuery 版本包含在我的版本之前,那么我最终会在调用 noconflict 之前覆盖它。
请帮忙
I am developing an ASP.NET server control that uses jQuery for some client side logic. I have embedded the jQuery file as a resource inside the control.
I don't want to restrict the application using the control to that specific version of jQuery and I want to keep using the version of jQuery that I have embedded.
I know about the noconflict method but the problem that i see with that is that i have no control over the order of the script tags on the page.
If the user's version of jQuery is included before mine than I'll end up overriding it before I can call noconflict.
Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
noConflict(true)
来执行此操作:true
参数告诉 jQuery 除了$
符号之外还释放jQuery
符号。只需将其添加到要嵌入控件中的 jQuery.js 文件的末尾即可。jQuery 脚本对于冲突很聪明。它所做的第一件事就是获取
$
和jQuery
的当前值并将它们存储起来,以便稍后在您要求时可以恢复它们。因此,如果您的脚本首先加载,则$
和jQuery
都不会被定义,并且新脚本可以拥有它们。如果您的脚本是第二个加载的,它恢复之前的$
和jQuery
。示例:
假设您使用的是最新版本 (v1.5.1),但页面作者使用的是较旧的 1.4.4。实际上,通过在 1.5.1 文件末尾添加
var jq151 = jQuery.noConflict(true);
,您可以执行以下操作:...除非它全部位于一个脚本标记中。所以有两种可能性:
1)他们先走:
实例
2)你先走:
实例
不管怎样,
jQuery
和$
最终都指向它们的 1.4。 4 版本,jq151
最终指向您的 1.5.1 版本。也许有点偏离主题,但对于任何认为这有点神奇的人来说,这实际上非常简单。 :-) 这是一个脚本,它将重新定义
foo
,但如果您要求它恢复以前的定义:上面定义的
foo
示例foo
示例> 在上述内容之后定义(如果您想知道为什么尽管var foo
位于脚本之后,但它仍然有效,这里有一些阅读内容,阅读有关贫穷的、被误解的var
)关于插件:您在下面询问了有关插件的问题。插件通过将其功能分配给
jQuery.fn
和(在某些情况下)jQuery
上的属性来注册自己,如下所示:通过上述操作,您可以访问
makeFoo
函数(例如,$('foo').makeFoo();
)。一个编写良好的插件将确保它通过使用以下结构与noConflict()
和noConflict(true)
良好地配合:。 ..或者一个喜欢的人。 (通过上面的内容,我们永远不会在函数体内使用
jQuery
来引用 jQuery。如果我们愿意,我们可以添加var jQuery = $;
在顶部。)它定义了一个匿名函数并立即调用它,传入
jQuery
的当前全局值。它接收作为$
参数,因此在函数内,符号$
将始终为jQuery 实例。它可以自由地使用
$
,因为它知道它引用了它注册的 jQuery 版本。一个编写得稍微好的插件可能会假设
jQuery
总是相同的(例如,只能与noConflict()
配合使用,而与noConflict(真)
)。不过,你可以解决这些问题。如果您遇到这样的情况,请复制一份并将...放在顶部,
...放在底部。 99% 的情况下,这都会让它表现良好。
如果您想在嵌入式 jQuery 实例中使用插件,最好的选择是将它们包含在您的自定义文件中。所以文件内容变成:
var jq151 = jQuery.noConflict(true);
You can do this with
noConflict(true)
:The
true
parameter tells jQuery to release thejQuery
symbol in addition to the$
symbol. Just add this to the end of the jQuery.js file you're embedding in the control.The jQuery script is smart about conflicts. The first thing it does is grab whatever the current value of both
$
andjQuery
are and squirrel them away so it can restore them later if you ask it to. So if your script is loaded first, neither$
norjQuery
will be defined and the new one can have them. If your script is loaded second, it restores the earlier$
andjQuery
.Example:
Let's assume you're using the latest (v1.5.1), but the page author is using the older 1.4.4. Effectively, by tacking
var jq151 = jQuery.noConflict(true);
on the end of the 1.5.1 file, you're doing this:...except it'd all be in one script tag. So two possibilities:
1) They go first:
Live example
2) You go first:
Live example
Either way, both
jQuery
and$
end up pointing to their 1.4.4 version, andjq151
ends up pointing to your 1.5.1 version.Perhaps slightly off-topic, but for anyone thinking this is a bit magical, it's actually really easy. :-) Here's a script that will redefine
foo
, but restore the previous definition if you ask it to:Example with
foo
defined before the aboveExample with
foo
defined after the above (if you're wondering why this works despitevar foo
being after the script, here's some reading on read up on poor, misunderstoodvar
)About plug-ins: You asked below about plug-ins. Plug-ins register themselves by assigning their features to properties on
jQuery.fn
and (in some cases)jQuery
, like so:With the above, you can access a
makeFoo
function on jQuery instances (e.g.,$('foo').makeFoo();
). A well-written plug-in will ensure that it plays nicely with bothnoConflict()
andnoConflict(true)
by using this structure:...or one like it. (With the above, we'd never use
jQuery
to refer to jQuery within the function body. If we wanted to, we could addvar jQuery = $;
at the top.)That defines an anonymous function and immediately calls it, passing in the current global value for
jQuery
. It receives that as a$
argument, and so within the function the symbol$
will always be thejQuery
instance that it passed into itself. It can freely use$
knowing that it refers the the version of jQuery on which it's registered.An only somewhat well-written plug-in may assume that
jQuery
will always be the same (e.g., only plays nicely withnoConflict()
and not withnoConflict(true)
). You can fix those, though. If you run into one, make a copy of it and put...at the top and
...at the bottom. 99% of the time, that will make it behave.
If you want to use plug-ins with your embedded jQuery instance, your best bet is to include them in your customized file. So the file contents become:
var jq151 = jQuery.noConflict(true);
这是一个很好的解决方案:不要使用 jQuery。至少——一开始不是。使用javascript等待所有脚本加载完毕,存储“$”的值,然后通过script标签注入jQuery,并恢复“$”之前的值。
Here's a nice solution: Don't use jQuery. At least - not at first. Use javascript to wait until all scripts have loaded, store the value of "$", and then inject jQuery in via script tag, and restore the previous value of "$".