weixin_33693070 2013-09-06 15:32 采纳率: 0%
浏览 73

2个版本的jquery如何保证不冲突?

我必须加载2个版本的jQuery,以便进行onClick事件和表单POST事件。onclick事件可以调用http code.jquery.com/jQuery-latest.min.js,但除非我将http code.jquery.com/jQuery-1.4.2.min.js称为a,否则POST事件将无法工作。 http://code.jquery.com/ajax/jquery.validate/1.7/jquery.validate.min.js但我必须确保两个版本无冲突才能让事情正常运作。

对latest.min的调用使onClick事件加载到了div中,而对submitHandler:function(Form)的调用使我的表单POST工作,后者加载到另一个div中。

菜单由onclick事件支持,并将正确的菜单加载到顶部栏中。然后,用户从该菜单的下拉菜单中选择,将表单加载到下面的另一个div中。

表单将被发布,并在div中为我显示表单所在的结果,并重新加载表单。

加载到div中的表单也具有表单工作的代码。

问题是,在它工作过一次之后,如果试图发布再次加载的相同表单(与第一个相同,包括发布表单的代码),它将无法发布。

投寄表格所用的脚本:

$.noConflict();
$(document).ready(function () {
    $("#ncus").validate({
        debug: false,
        submitHandler: function (form) {
            $.post('../AddCusForm/process_form.php', $("#ncus").serialize(), function (data) {
                $('#mainBody').html(data);
                $("#mainBody").find("script").each(function (i) {
                    eval($(this).text());
                });
                $("form#ncus")[0].reset();
            });
        }
    });
});

加载到同一个div中的进程表单具有与上面相同的代码,并将原始表单替换为另一个表单(同名)。

  • 写回答

1条回答 默认 最新

  • weixin_33696106 2013-09-06 15:37
    关注

    Your code is releasing the $ variable each time $.noConflict is called. The first time, that reverts you back one version of jQuery; the second time, it probably makes $ undefined.

    Ideally, what you want to do is use only a single version of jQuery.

    Sometimes for various reasons that's not possible in the short term. For those situations:

    The correct way to use multiple jQuery versions on a page is to use the return value from noConflict and/or a scoping function.

    Here's the return value example:

    <script src="jquery-vX.js"></script>
    <script src="some-plugin-that-requires-vX.js"></script><!-- (if any) -->
    <script>
    var $X = jQuery.noConflict(true);
    </script>
    <script src="jquery-vY.js"></script>
    <script src="some-plugin-that-requires-vY.js"></script><!-- (if any) -->
    <script>
    var $Y = jQuery.noConflict(true);
    </script>
    

    Then use $X for jQuery version X, and $Y for jQuery version Y. Note that the plugins you load will hook themselves up to the then-current version of jQuery.

    Note the order of things above:

    1. Include the file for the jQuery version.
    2. Include any plug-ins that you need to use with that version.
    3. Then use noConflict (once).

    The order is important because a properly-written plug-in will use the jQuery symbol to hook itself up, so you need to make sure that jQuery is pointing at the right version when you load the plug-in.

    You can also use a scoping function so you can use a given version with $ within that function:

    (function($) {
        // Here, $ will be whatever you pass in below, in this example, version X
        $(...).doSomethingWithVersionX();
    })($X);
    (function($) {
        // Here, $ will be whatever you pass in below, in this example, version Y
        $(...).doSomethingWithVersionY();
    })($Y);
    

    If you don't like the way the version is buried at the bottom, this works just as well:

    (function() {
        var $ = $X; // <== Note, jQuery version X
    
        // Here, $ will be whatever you assigned above
        $(...).doSomethingWithVersionX();
    })();
    (function() {
        var $ = $Y; // <== Note, jQuery version Y
    
        // Here, $ will be whatever you assigned above
        $(...).doSomethingWithVersionY();
    })();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题