如何验证 HTMLLIElement 是否被 jQuery 隐藏

How to validate if an HTMLLIElement is hidden with jQuery

本文关键字:是否 jQuery 隐藏 HTMLLIElement 何验证 验证      更新时间:2023-09-26

我正在遍历一个名为content的变量,它包含几个HTMLLIElement对象。我如何将jQuery或JavaScript的函数用于这个对象?,我试图做的是用注释代码编写的那种验证。

    $.each(content, function(index, value){
         //if(!value.is(':hidden')){
              console.log(index + ' : ' + value);
         //}
    });

我得到的是

未捕获的类型错误:对象 # 没有方法"is"

如果我这样做value.getAttribute('style');我会得到'display: none;'

$.each函数中的第二个参数引用 DOM HTMLLIElement元素。要对其应用 jQuery 方法(如 is),您必须将 value 元素包装在 jQuery 对象中:

if(!$(value).is(':hidden')) {

小提琴

正如@anonymousdownvotingislame所指出的,if($(value).is(':visible'))可能更具可读性,因为人脑往往难以解释双重否定,更不用说您不必使用不!运算符了。谢谢。 =]

Value 不是 jQuery 对象。

尝试:

$.each(content, function(){
         if($(this).is(':hidden')){
              console.log(index + ' : ' + value);
         }
});

尝试:

$content.each(function() {
    if ($(this).is(":hidden")) {
        console.log("whatever");
    }
});

。或者只是做:

$.each(content, function(index, value) {
    if (value.getAttribute("style") === "display: none;") {
        consoloe.log("whatever");
    }
});