同时运行多个版本的jQuery [英] Running Multiple versions of jQuery at the same time

查看:77
本文介绍了同时运行多个版本的jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要运行两个版本的jQuery.我尝试将j别名为jQuery.这是我的代码.

I need to run two versions of jQuery. I've tried aliasing j to jQuery. Here is my code.

j = jQuery.noConflict(); // No conflict with older version of jQuery
(function($) {
  $('#any_id_or_class').on("click", function(event){
    console.log('clicked');
  });
})(j);

我已经使用$ .fn.jquery记录了jQuery的版本,并且我得到了两个不同的版本,具体取决于我是从函数内部还是在范围之外进行记录.如果单击文档$('body')的'body',则会得到输出,但是这就是所有问题的所在,如果我在页面上使用任何类或id,则什么都不会发生?想法?

I've logged the version of jQuery using $.fn.jquery and I get two distinct versions depending if I log from inside my function or outside the scope. If I put a click to the 'body' of the document $('body') I get output, but here's where it all goes wrong, if I use any class or id on the page nothing happens? Ideas?

显然,我只想使用一个版本的jQuery,但这不是一个选择.

Edit 1: Obviously I would like to use only one version of jQuery, but that isn't an option.

这是我如何使其工作而不理想,因为我想尝试在函数范围内对$进行别名.

Edit 2: Here's how I got it working, not ideal, as I wanted to try and alias the $ within the scope of the function.

j = jQuery.noConflict(); // No conflict with older version of jQuery

j(document).ready(function(){
  j('#masthead').on("click", function(event){
    console.log('clicked');
  });
});

推荐答案

var j = jQuery.noConflict(); // Unbinds the last included version of Jquery

  j('#any_id_or_class').on("click", function(event){
    console.log('clicked');
  });

noConflict分配将注销最后一个包含的jquery文件,因此请确保最后一个包含的是使用.on的新版本.

The noConflict assignment will de-register the last included jquery file, so make sure the last included one is the new version to work with .on

这篇关于同时运行多个版本的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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