【问题标题】:Need to use 2 versions of jquery 1.7.2 and 1.8.2需要使用jquery 1.7.2和1.8.2 2个版本
【发布时间】:2014-03-26 14:48:15
【问题描述】:

我有一个冲突问题,我需要在同一页面上使用两个版本的 jquery,需要 1.7.2 是我用来根据选择更改从数据库中的 php 数据中的其他页面获取的函数,我需要 1.8.2 来进行表单验证,但我只能做一个或另一个,如果我尝试这两个不起作用。

我尝试使用 jquery noconflict 但我不太明白,也许我做错了或不起作用。

需要1.7.2的功能:

            function getmes(dropdown)
   {
     ano = dropdown.options[dropdown.selectedIndex].value;
    $.ajax({
            url: "ajax/mes.php",
            dataType: "html",
            type: 'GET',
            async: true,
            data: {
                ano: ano
            },
            success: function (result) {
                $("#mes").append(result.replace(/[\r\n]+(?=[^\r\n])/g,''));
            }
            });
    }

这是需要 1.8.2 的功能:

var dataForm = new VarienForm('form-id', true);

【问题讨论】:

  • 真的看起来像一个 XY 问题。您可能需要 是确保您的所有代码都能与最新的 jquery 一起使用。
  • 为新版本重构旧代码
  • 当您在声称需要 1.7.2 的功能上使用 1.8.2 时,您会遇到什么错误?在发布的代码中,我看不出与较新版本有什么冲突。
  • 最好的解决方案是不再需要两个单独的版本。真的不应该有需要这个的理由。

标签: javascript php jquery validation conflict


【解决方案1】:

如果绝对必要,您可以这样做:

<script src="jquery.1.7.2.js"></script>
<script>
/* Here, you can use $ with your jQuery 1.7.2 code, 
ie. use jQuery 1.7.2 as you normally would.*/
jQuery172 = jQuery.noConflict(true);
/*Now, you can no longer use $. Use jQuery172 with your jQuery 1.7.2
code here, ie. replacing "$" with "jQuery172" in your code.*/
</script>
<script src="jquery.1.8.2.js"></script>
<script>
/*Here, you can use $ again, but now with your jQuery 1.8.2 code, 
ie. use jQuery 1.8.2 as you normally would.*/
jQuery182 = jQuery.noConflict();
/*Now, you can no longer use $. Here, you can use jQuery172 with your 
jQuery 1.7.2 code and use jQuery182 with your jQuery 1.8.2 code.*/
</script>

但是,我建议重构您的代码以支持较新的版本。加载两个版本的 jQuery 意味着每次页面加载的带宽增加一倍。这对您的服务器(除非您使用 CDN)和客户端都是一个负担。此外,浏览器将不得不构造 jQuery 对象两次,这对性能肯定是不利的,尽管在较新的计算机上可能不明显。

【讨论】:

  • 请注意,这个解决方案在实施时实际上会非常繁重。考虑这是最后的手段,或用于测试目的。 (使用 Google 托管库也可以满足您的带宽需求:developers.google.com/speed/libraries/devguide#jquery
  • 对不起,我是 jquery 的新手,但请解释一下,我如何在代码中使用该 JQuery172?我用它代替 $?
  • 是的,像这样:jQuery172.ajax({ ... })。当然,您可以选择不同的变量名称,例如。 7 美元。
  • muito obrigado gente consegui descobrir o que era que tava dando conflito usando o console do firebug e apliquei o que o alethes disse so neles e funciono agora
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-09
相关资源
最近更新 更多