JQuery 样式显示值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1189259/
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 10:52:01  来源:igfitidea点击:

JQuery style display value

jquerycss

提问by kaivalya

How can I check the display value of an element

如何检查元素的显示值

<tr id="pDetails" style="display:none">

$("tr[id='pDetails']").attr("style")gives me 'display:none'

$("tr[id='pDetails']").attr("style")给我 'display:none'

I want to write a jquery script that will return me only the value of display which is 'none'

我想编写一个 jquery 脚本,它只会返回显示的值,即“无”

Is that possible?

那可能吗?

回答by seth

Just call csswith one argument

只需用一个参数 调用css

  $('#idDetails').css('display');

If I understand your question. Otherwise, you want cletus' answer.

如果我理解你的问题。否则,您需要 cletus 的答案。

回答by cletus

Well, for one thing your epression can be simplified:

嗯,一方面,你的情绪可以被简化:

$("#pDetails").attr("style")

since there should only be one element for any given ID and the ID selector will be muchfaster than the attribute id selector you're using.

因为应该只有一个元素对于任何给定ID和ID的选择会很多比你正在使用的属性id选择速度更快。

If you just want to return the display value or something, use css():

如果只想返回显示值什么的,使用css():

$("#pDetails").css("display")

If you want to search for elements that have display none, that's a lot harder to do reliably. This is a rough example that won't be 100%:

如果您想搜索没有 display none 的元素,要可靠地做到这一点要困难得多。这是一个粗略的例子,不会是 100%:

$("[style*='display: none']")

but if you just want to find things that are hidden, use this:

但如果您只想找到隐藏的东西,请使用以下命令:

$(":hidden")

回答by jspek

If you want to check the display value, https://stackoverflow.com/a/1189281/5622596already posted the answer.

如果您想检查显示值,https://stackoverflow.com/a/1189281/5622596已经发布了答案。

However if instead of checking whether an element has a style of style="display:none"you want to know if that element is visible. Then use .is(":visible")

但是,如果不是检查元素是否具有样式,style="display:none"而是想知道该元素是否可见。然后使用.is(":visible")

For example: $('#idDetails').is(":visible");

例如: $('#idDetails').is(":visible");

This will be trueif it is visible & falseif it is not.

这将是true如果它是可见的,false如果它不可见。

回答by Elzo Valugi

This will return what you asked, but I wouldnt recommend using css like this. Use external CSS instead of inline css.

这将返回您所问的内容,但我不建议使用这样的 css。使用外部 CSS 而不是内联 CSS。

$("tr[id='pDetails']").attr("style").split(':')[1];