我在Chrome开发控制台得到以下错误:

Uncaught TypeError: Cannot read property 'msie' of undefined

我的理解是,这是因为.browser现在在jQuery中已弃用,但我使用的是最新版本的jQuery工具,它仍然会给出错误,我检查了js文件,它就在那里。

我怎样才能避免这个错误呢?


当前回答

这个问题主要是由浏览器导航和版本属性引起的。 为此,你必须在函数中添加以下代码:

/* solution to undefined msie */
jQuery.browser = {};
jQuery.browser.msie = false;
    jQuery.browser.version = 0;
    if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
        jQuery.browser.msie = true;
        jQuery.browser.version = RegExp.$1;
    }
/* solution to undefined msie */    

例如,我正在使用它的点击功能,看到它:

 $(document).on('click', '.remove_add_test', function() {
    var product_id = $(this).data('id');
    var thisproduct = $(this);
    
    /* solution to undefined msie */
    jQuery.browser = {};
    jQuery.browser.msie = false;
        jQuery.browser.version = 0;
        if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
            jQuery.browser.msie = true;
            jQuery.browser.version = RegExp.$1;
        }
    /* solution to undefined msie */    
    
    jConfirm('Are you sure you want to delete record?', 'Remove Product', function(r) {
        if (r == true) {
            $.ajax({
                type: 'POST',
                url: 'scripts/ajax/index.php',
                data: {
                    method: 'removeproduct_fromcart',
                    id: product_id
                },
                dataType: 'json',
                success: function(data) {
                    if (data.RESULT == 0) {
                        $(".price span").text(data.totalprice);
                        $(thisproduct).closest('tr').remove();
                        $(".cart-number").text(data.productcount - 1);
                        $.growl.notice({
                            title: "Shopping Cart",
                            message: data.MSG
                        });
                       location.reload();
                        // window.location.href = 'https://www.youth-revisited.co.uk/payment.html';

                    }
                    // window.location.href = 'https://www.youth-revisited.co.uk/payment.html';
                }
            });
            // window.location.href = 'https://www.youth-revisited.co.uk/payment.html';

        } else {
            return false;
        }
    });
});

其他回答

你可以看看AJ的解决方案。这非常简单,只需复制并粘贴下面的代码行。

jQuery.browser = {};
(function () {
    jQuery.browser.msie = false;
    jQuery.browser.version = 0;
    if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
        jQuery.browser.msie = true;
        jQuery.browser.version = RegExp.$1;
    }
})();

参考:

美元。jQuery 1.9浏览器。x表示遗留IE检测

像吹一样吹

我使用这个命令并求解

未捕获类型错误:无法读取未定义的属性“msie

if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.1/.test($.fn.jquery)) {
    return;
}

将JS替换为

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

来源链接

美元的。jQuery 1.9已经删除了browser方法。

jQuery.browser()删除 jQuery.browser()方法自jQuery 1.3起已弃用,并在1.9中被移除。如果需要,它可以作为jQuery Migrate插件的一部分使用。我们建议在Modernizr这样的库中使用特征检测。 - jQuery Core 1.9升级指南。

如升级指南中所述,您可以尝试使用jQuery Migrate插件来恢复此功能,并让jQuery工具工作。

如果你定义了两次jQuery,那么你就会得到这个错误。例如,如果你正在使用Primefaces(它已经包含了jQuery),而你在其他地方定义了它。