【问题标题】:Unable to use jquery plugin using higher version of jquery, while loading two versions of jquery simultaneously无法使用更高版本的jquery的jquery插件,同时加载两个版本的jquery
【发布时间】:2012-02-06 10:01:47
【问题描述】:

我们的项目在 jquery 1.3.2 上运行,是的,我知道是一个非常旧的版本。无论如何,我们现在不打算升级它。我们现在只需要使用jquery的chosen plugin(下载链接-https://github.com/harvesthq/chosen),至少需要jquery 1.4。因此,我按照以下解决方案在同一页面上加载两个版本的 jquery -

https://stackoverflow.com/a/1566644/351903

但我无法在较新版本的 jquery 上调用 selected。这就是我所做的 -

1.在 jquery 加载部分

<script type="text/javascript" src="/js/jquery.js"></script>

<!-- Added for using jQuery chosen plugin -->
<script type="text/javascript" src="/js/jquery-1.4.min.js"></script>
<script type="text/javascript">
    var jQuery_1_4 = $.noConflict(true);
</script> 

注意 - 我没有为旧版本的 jquery 编写以下内容,因为我不想用 jQuery_1_3_2("something").somefunc() 替换我写过类似 $("something").somefunc() 的任何内容 -

<script type="text/javascript">
var jQuery_1_3_2 = $.noConflict(true);
</script>

2.然后,在插件调用部分

<link rel="stylesheet" type="text/css" href="/css/chosen.css"/>
<!--  <script type="text/javascript" src="/js/jquery.chosen/chosen.jquery.min.js"></script> -->
<script type="text/javascript" src="/js/jquery.chosen/chosen.jquery.js"></script>
<script type="text/javascript">
jQuery_1_4(document).ready(function() 
{
    jQuery_1_4("select#user_id").chosen();  //Trying to call chosen on the newer version of jquery
});
</script>

但我收到此错误jQuery_1_4("select#user_id").chosen is not a function

但是,这可以正常工作 - alert("see"+jQuery_1_4("select#user_id").attr("id"));

所以,问题似乎只是在使用插件时,可能我必须在插件代码中做一些调整,可能是用jQuery_1_4 替换一些$s?但这听起来不太好,我不确定什么是正确的方法。

【问题讨论】:

    标签: jquery plugins multiple-instances multiple-versions


    【解决方案1】:

    如果您想将旧版本保留为默认值,您应该在加载旧版本后立即执行var jQuery = $.noConflict(true);。但是,您仍然需要检查您的$("something").somefunc() 电话

    【讨论】:

    • 谢谢。似乎应该首先包含较新的版本,如 web.enavu.com/daily-tip/… 中所述。我只需要在我的插件文件中编辑一行 - $ = jQuery;$ = jQuery_1_4;。而且我想我以前所有的 jquery 都工作正常。我只是随机检查了一些,它们正在工作。
    【解决方案2】:

    在包含第二版 jQuery 之前调用 noConflict()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      相关资源
      最近更新 更多