6

</body>我有一个 HTML 文档,其中我在thickbox 的标题中引用了 jQuery 版本 1.2.2,后来我在用于图片幻灯片的标签之前引用了 jQuery 1.7.1 。

问题是,除非删除 jQuery 1.7.1 的引用,否则厚框将无法工作,这会导致幻灯片停止工作。

我用谷歌搜索了有关$冲突的信息,但建议的解决方案都没有奏效。

我见过并尝试过的最常见的是:var $j = jQuery.noConflict();

我该如何解决这个问题?

4

5 回答 5

2

If the plug-ins are well-behaved, then this should work:

<script src="jquery-1.2.2.js"></script>
<script src="thickbox.js"></script>
<script src="jquery-1.7.1.js"></script>
<script src="slideshow.js"></script>

(Obviously those script names are made up.) Here's an example (source) (I used jQuery 1.4.2 and jQuery 1.7.1 because Google doesn't host 1.2.2).

The above works with well-behaved plug-ins because a well-behaved plug-in doesn't rely on the global $ at all, but rather uses the value of the jQuery global as of when it was loaded and grabs a reference to it in a closure, then uses that local reference throughout the plug-in's code, like this:

// Example plug-in setup
(function($) {
    // ...Plug-in code using `$` here -- note it's a *local* `$`,
    // not the global `$`, and not the global `jQuery`...
})(jQuery);

or

(function() {
    var $ = jQuery;

    // ...Plug-in code using `$` here -- note it's a *local* `$`,
    // not the global `$`, and not the global `jQuery`...
})();

Both of those grab the global jQuery value as of when the plug-in is loaded and then use their local alias throughout.

If the plug-in wants to wait for the ready event, it can also do this:

jQuery(function($) {
    // ...Plug-in code using `$` here -- note it's a *local* `$`,
    // not the global `$`, and not the global `jQuery`...
});

...which uses the jQuery function passed into the ready handler.

Any of those three would work correctly (with thickbox seeing jQuery 1.2.2, and slideshow seeing jQuery 1.7.1) with the script load order listed above.

But the "if" in my opening sentence is a big "if". A lot of plug-ins are not written to be bullet-proof in this way.


The above notwithstanding, I would migrate away from any plug-in that requires jQuery 1.2.2 in order to work, and wherever possible (and it's almost always possible) avoid having to load two different versions of any library, including jQuery, in the same page.

于 2012-09-11T12:22:27.433 回答
2
<script src="http://code.jquery.com/jquery-1.2.2.min.js"></script>
<script type="text/javascript">
  var jQ122 = jQuery.noConflict();
</script>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
  var jQ171 = jQuery.noConflict();


  (function($) { 
    $(function() {
      // here $ is jQuery 1.2.2
    });
  })(jQ122);


  (function($) { 
    $(function() {
      // here $ is jQuery 1.7.1
    });
  })(jQ171);
</script>
于 2012-09-11T12:24:49.513 回答
0

尽管您可以使用jQuery.noConflict();同时声明不同版本的 jQuery。

当我这样做时,我经常在某些库的 IE8 上遇到一些问题。

因此,另一种解决方案是使用 iframe 在当前页面中加载页面。

在一个页面上有一个给定版本的 jQuery,在第二个页面上有另一个。

例如:

<iframe frameborder="0" width="700" height ="400" scrolling="no" seamless="seamless" src="your.html"></iframe>
于 2012-09-14T22:12:27.670 回答
0

我不建议使用两个不同版本的 jQuery。还有其他一些可以与最新的 jQuery 完美配合的thickbox 替代品。

于 2012-09-11T12:24:30.473 回答
0

您想要做的是一种非常糟糕的做法(您确实应该将所有代码迁移到同一版本)但理论上可以做到......

无论如何,您都需要对相应的插件进行更改...考虑以下代码:

<script src="jquery-1.4.js"></script>
var jQuery14 = jQuery;
<script src="jquery-1.7.js"></script>
var jQuery17 = jQuery;

然后,您将在将 jQuery 交给插件的地方更改插件的代码,这看起来类似于:

(function( $ ){
     // all your plugins code would be here
})( jQuery );     // replace "jQuery" with one of your respective jQuery14/jQuery17 versions/variables

被告知..这至少可以说非常非常混乱!编写干净的代码或稍后付款!:)

于 2012-09-11T12:28:05.377 回答