首页 > 解决方案 > 使用 .has() 时 jQuery 不是函数

问题描述

我有这个简单的 jQuery 代码

function removePreloader() {
    jQuery('ul.woocommerce-error').has('li').jQuery("#preloader").css("display", "hidden");
}

它被调用

jQuery('form[name="checkout"]').submit(function(e) {
    ... // lots of line
    setTimeout(removePreloader(), 2000);
}

两个代码块都在里面jQuery(document).ready(function() { ... });

另一个jQuery()工作正常,只有这个导致问题并显示

未捕获的类型错误:jQuery(...).has(...).jQuery 不是函数

不能用.has吗?还是有替代品?因为这个wordpress主题使用了很多老插件,所以他们不能接受新版本的jQuery。

谢谢

这是来自的屏幕截图jquery.com

在此处输入图像描述

我只是想遵循这个 javascript 并对其进行一些修改,请让我知道如何以正确的方式执行此操作,因为我以前从未使用 javascript 编码

标签: javascriptjquery

解决方案


您正在使用无效的 jQuery 语句.jQuery...,我建议if在检查列表中是否有任何子项时使用该语句,li例如:

function removePreloader() {
    if( jQuery('ul.woocommerce-error li').length ){
       jQuery("#preloader").css("display", "none");
    }
}

注意 1: display属性没有hidden价值,因此您正在搜索none

注意2:删除()函数调用中的,如:

setTimeout(removePreloader, 2000);

推荐阅读