jQuery的版本冲突解决与ASP.NET服务器控件 [英] jQuery version conflict resolution with ASP.NET Server Control

查看:100
本文介绍了jQuery的版本冲突解决与ASP.NET服务器控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发使用jQuery一些客户端逻辑ASP.NET服务器控件。我已经嵌入了jQuery文件作为内部控制的资源。

我不希望限制使用控制的jQuery的特定版本的应用程序,我想用我已经嵌入了jQuery的版本保持。

我了解noconflict方式,但我与看到的问题是,我有过脚本标记的页面上的秩序无法控制的。

如果用户的jQuery的版本是我的比我将最终覆盖它之前,我可以调用noconflict之前。

请帮忙


解决方案

您可以用 <$做到这一点C $ C> noConflict(真)

  VAR myJQuery = jQuery.noConflict(真);

真正参数告诉jQuery来释放的jQuery 符号,除了 $ 符号。只是这添加到的jquery.js文件的末尾你在控制嵌入。

jQuery的脚本是聪明的冲突。它做的第一件事就是抢无论两者的当前值 $ 的jQuery 是和松鼠他们走那么它可以如果你问它以后恢复它们。所以,如果你的脚本第一次加载,既不 $ 也不的jQuery 将被定义和新的人能够拥有它们。如果你的脚本加载第二个,它的恢复的早期 $ 的jQuery

示例

让我们假设你正在使用最新版本(V1.5.1),但页面笔者使用的是较旧的1.4.4。实际上,通过套结 VAR jq151 = jQuery.noConflict(真); 在1.5.1文件的末尾,你这样做是:

 &LT;脚本的src ='的jquery-1.5.1.min.js'&GT;&LT; / SCRIPT&GT;
&LT;脚本&GT; VAR jq151 = jQuery.noConflict(真);&LT; / SCRIPT&GT;

...除了它会全部是在一个脚本标签。所以两种可能性:

1)他们先走:

 &LT;脚本的src ='的jquery-1.4.4.min.js'&GT;&LT; / SCRIPT&GT;
&LT;脚本的src ='的jquery-1.5.1.min.js'&GT;&LT; / SCRIPT&GT;
&LT;脚本&GT; VAR jq151 = jQuery.noConflict(真);&LT; / SCRIPT&GT;

活生生的例子

2)你先走:

 &LT;脚本的src ='的jquery-1.5.1.min.js'&GT;&LT; / SCRIPT&GT;
&LT;脚本&GT; VAR jq151 = jQuery.noConflict(真);&LT; / SCRIPT&GT;
&LT;脚本的src ='的jquery-1.4.4.min.js'&GT;&LT; / SCRIPT&GT;

活生生的例子

无论哪种方式,都的jQuery $ 最终指向自己的1.4.4版本,而 jq151 最终指向您的1.5.1版本。


也许稍微偏离主题,但任何人都以为这是有点神奇,它实际上很容易。 :-)这里有一个脚本,将重新定义,但如果你问它来恢复previous定义:

  //脚本
(函数(){
    VAR globalObject =这个,//或者只是使用`window`上的浏览器
        oldfoo,
        ourfoo;    oldfoo = globalObject.foo;
    ourfoo = globalObject.foo = {
        版本:新,
        恢复previous:恢复previous
    };    功能恢复previous(){
        globalObject.foo = oldfoo;
        返回ourfoo;
    }
})();

例之前,上述

例以上之后定义(如果你想知道为什么这个作品尽管 VAR富是的之后的剧本,的这里的一对贫穷,误解了 VAR读了)<一些阅读 / p>


关于插件:你下面问插件。插件注册自己通过 jQuery.fn 的分配功能,性能和(某些情况下)的jQuery ,像所以:

  jQuery.fn.makeFoo =功能(){
};

通过上述,您可以访问jQuery的实例的 makeFoo 功能(例如, $('富')makeFoo(); )。 A 写得很好的插件将确保其符合 noConflict() noConflict(真)<起到很好/ code>通过采用这种结构:

 (函数($){
    $ .fn.makeFoo =功能(){
        $(本).addClass(富);
    };
})(jQuery的);

...或者一个喜欢它。 (以上所述,我们会的从不的使用的jQuery 来在函数体内引用jQuery的。如果我们想,我们可以添加 VAR的jQuery = $; 在顶部)

这定义了一个匿名函数,并立即调用它,在当前全球价值传递的的jQuery 。它接收,作为一个 $ 参数的,所以在函数中的符号 $ 将的总是的是的jQuery 实例,它传递到自身。它可以自由使用 $ 知道它是指在其上的注册的jQuery的版本。

这是唯一有点精心编写插件可以假设的jQuery 将永远是相同的(例如,仅与 noConflict很好的播放() 而不是 noConflict(真))。您可以修复这些,但。如果碰上之一,请它的一个副本,并把

 (函数($){
    VAR的jQuery = $;

...的顶部和

 })(jQuery的);

...在底部。的99%的时间,这将使它的行为。

如果你想使用插件与您的嵌入式jQuery的情况下,最好的办法是将它们包括的的自定义文件。因此,该文件的内容变成了:


  • jQuery的脚本

  • (插件脚本)

  • (插件脚本)

  • ...

  • VAR jq151 = 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

解决方案

You can do this with noConflict(true):

var myJQuery = jQuery.noConflict(true);

The true parameter tells jQuery to release the jQuery 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 $ and jQuery are and squirrel them away so it can restore them later if you ask it to. So if your script is loaded first, neither $ nor jQuery will be defined and the new one can have them. If your script is loaded second, it restores the earlier $ and jQuery.

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:

<script src='jquery-1.5.1.min.js'></script>
<script>var jq151 = jQuery.noConflict(true);</script>

...except it'd all be in one script tag. So two possibilities:

1) They go first:

<script src='jquery-1.4.4.min.js'></script>
<script src='jquery-1.5.1.min.js'></script>
<script>var jq151 = jQuery.noConflict(true);</script>

Live example

2) You go first:

<script src='jquery-1.5.1.min.js'></script>
<script>var jq151 = jQuery.noConflict(true);</script>
<script src='jquery-1.4.4.min.js'></script>

Live example

Either way, both jQuery and $ end up pointing to their 1.4.4 version, and jq151 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:

// The script
(function() {
    var globalObject = this, // Or just use `window` on browsers
        oldfoo,
        ourfoo;

    oldfoo = globalObject.foo;
    ourfoo = globalObject.foo = {
        version: "new",
        restorePrevious: restorePrevious
    };

    function restorePrevious() {
        globalObject.foo = oldfoo;
        return ourfoo;
    }
})();

Example with foo defined before the above

Example with foo defined after the above (if you're wondering why this works despite var foo being after the script, here's some reading on read up on poor, misunderstood var)


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:

jQuery.fn.makeFoo = function() {
};

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 both noConflict() and noConflict(true) by using this structure:

(function($) {
    $.fn.makeFoo = function() {
        $(this).addClass("foo");
    };
})(jQuery);

...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 add var 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 the jQuery 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 with noConflict() and not with noConflict(true)). You can fix those, though. If you run into one, make a copy of it and put

(function($) {
    var jQuery = $;

...at the top and

})(jQuery);

...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:

  • The jQuery script
  • (plug-in script)
  • (plug-in script)
  • ...
  • Your var jq151 = jQuery.noConflict(true);

这篇关于jQuery的版本冲突解决与ASP.NET服务器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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