jQuery 测试是否显示 = 无

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2975073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:38:55  来源:igfitidea点击:

test if display = none

jquery

提问by Tillebeck

This does not work, should it? Or can you stop the error if another line could do the same:

这不起作用,是吗?或者如果另一行可以做同样的事情,你能不能停止错误:

function doTheHighlightning(searchTerms) {
    // loop through input array of search terms
    myArray = searchTerms.split(" ");
    for(i=0;i<myArray.length;i++)
    {
        // works. this line works if not out commented. Will highlight all words, also in the hidden elements
        //$('tbody').highlight(myArray[i]);

        // not working when trying to skip elements with display none...
        $('tbody').css('display') != 'none').highlight(myArray[i]);
    }

    // set background to yellow for highlighted words
    $(".highlight").css({ backgroundColor: "#FFFF88" });
}

I need to filter rows in a table and color some word. The data has become way to much for the coloring if many words are chosen. So I will try to limit the coloring by only going through the none hidden elements.

我需要过滤表格中的行并为一些单词着色。如果选择了许多单词,则数据已成为着色的方式。因此,我将尝试通过仅通过非隐藏元素来限制着色。

回答by user113716

If you want to get the visible tbodyelements, you could do this:

如果你想获得可见tbody元素,你可以这样做:

$('tbody:visible').highlight(myArray[i]);

It looks similar to the answer that Agent_9191gave, but this one removes the space from the selector, which makes it selects the visible tbodyelements instead of the visible descendants.

它看起来类似于Agent_9191给出的答案,但是这个从选择器中删除了空间,这使得它选择可见tbody元素而不是可见的后代。



EDIT:

编辑:

If you specifically wanted to use a test on the displayCSS property of the tbodyelements, you could do this:

如果您特别想对元素的displayCSS 属性进行测试tbody,您可以这样做:

$('tbody').filter(function() {
     return $(this).css('display') != 'none';
}).highlight(myArray[i]);

回答by Zuul

Use like this:

像这样使用:

if( $('#foo').is(':visible') ) {
    // it's visible, do something
}
else {
    // it's not visible so do something else
}

Hope it helps!

希望能帮助到你!

回答by Agent_9191

Try this instead to only select the visible elements under the tbody:

试试这个,只选择 下的可见元素tbody

$('tbody :visible').highlight(myArray[i]);

回答by jAndy

$('tbody').find('tr:visible').hightlight(myArray[i]);

回答by Gabriele Petrioli

As @Agent_9191 and @partick mentioned you should use

正如@Agent_9191 和@partick 提到的,你应该使用

$('tbody :visible').highlight(myArray[i]); // works for all children of tbody that are visible

or

或者

$('tbody:visible').highlight(myArray[i]); // works for all visible tbodys

Additionally, since you seem to be applying a class to the highlighted words, instead of using jquery to alter the background for all matched highlights, just create a css rule with the background color you need and it gets applied directly once you assign the class.

此外,由于您似乎对突出显示的单词应用了一个类,而不是使用 jquery 来更改所有匹配的突出显示的背景,只需使用您需要的背景颜色创建一个 css 规则,并在您分配类后直接应用它。

.highlight { background-color: #FFFF88; }

回答by notoxics

You can use the following code to test if displayis equivalent to none:

您可以使用以下代码来测试是否display等效于none

if ($(element).css('display') === 'none' ){
    // do the stuff
}