我可以使用jQuery.noConflict使用任何版本的jquery

问题描述 投票:1回答:2

正在使用此插件https://github.com/blueimp/jQuery-File-Upload/wiki/Basic-plugin

但是我的jQuery版本是1.4,所以我使用了jQuery.noConflict(),因为有人说我可以通过此版本使用任何版本的jQuery:

var jq10 = jQuery.noConflict();

但是当我尝试使用该插件时,它将不起作用,它也不会出现错误,因此我不知道我的代码是否错误,或者即使使用jQuery.noConflict()也无法使用。有人知道吗?这是我正在做的示例,非常简单,但是它不起作用,或者没有任何错误可以给我提示

<html>
    <input type="file" name="files[]" id="fileupload" multiple>
</html>

//This is my original version
<script language="javascript" type="text/javascript" src="/scripts/jquery-1.4.2.min.js"></script> 
//This is the minimum version required of the plugin
<script type="text/javascript" src="/scripts/new_jquery/jquery-1.6.4.js"></script>
//These are the requirements for the plugin
<script language="javascript" type="text/javascript" src="/scripts/new_jquery/fileupload/js/vendor/jquery.ui.widget.js"></script>
<script language="javascript" type="text/javascript" src="/scripts/new_jquery/fileupload/js/jquery.iframe-transport.js"></script>
<script language="javascript" type="text/javascript" src="/scripts/new_jquery/fileupload/js/jquery.fileupload.js"></script>

$(document).ready( function() {
    var jq10 = jQuery.noConflict();

    jq10('#fileupload').fileupload({
        dataType: 'json',
        add: function (e, data) {
            console.log(data);
        }
    });
});

[您看,我只是在控制台上记录了数据,但是它不会在萤火虫中返回任何内容,不会有错误,也不会给我任何提示。

javascript jquery
2个回答
2
投票

尝试一下:

jQuery.noConflict();
// Do something with jQuery
jQuery( "div p" ).hide();
// Do something with another library's $()
$( "content" ).style.display = "none";

所以,

var jq10 = jQuery.noConflict();
jq10(document).ready( function() {

$('#fileupload').fileupload({
    dataType: 'json',
    add: function (e, data) {
        console.log(data);
    }
});
});

0
投票

用此替换所有内容:

<html>
<head>
    <title>Title</title>
</head>
<body>
    <input type="file" name="files[]" id="fileupload" multiple>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script type="text/javascript" src="/scripts/new_jquery/fileupload/js/vendor/jquery.ui.widget.js"></script>
    <script type="text/javascript" src="/scripts/new_jquery/fileupload/js/jquery.iframe-transport.js"></script>
    <script type="text/javascript" src="/scripts/new_jquery/fileupload/js/jquery.fileupload.js"></script>
    <script>
        $.noConflict();
        jQuery(document).ready(function ($) {
            // Code that uses jQuery's $ can follow here.
            $('#fileupload').fileupload({
                dataType: 'json',
                add: function (e, data) {
                    console.log(data);
                }
            });
        });// Code that uses other library's $ can follow here.
    </script>
</body>
</html>

首先,有人告诉你错了。如果使用$.noConflict(),则不能使用其他版本的jquery。 (确定,可以,但是不建议使用

但是请稍等!

您根本不应该加载两个版本的jquery。如果这样做,那就太荒谬了。

但是是

如果您确实必须使用过旧的插件或依赖于较旧的jquery版本。

© www.soinside.com 2019 - 2024. All rights reserved.