简体   繁体   English

检索内联CSS样式值

[英]Retrieving an inline CSS style value

I have a div that is styled automatically by a template based on variable results pulled from another data source. 我有一个div ,该div由模板根据从另一个数据源提取的可变结果自动设置样式。 The style value is set inline, like this: 样式值是内联设置的,如下所示:

<div class="view-empty message" style="display: block;">
    There are no active Invoices to display.
</div>

I've been able to see this in the DOM and toggle visibility back and forth, but so far have been unable to use Jquery to retrieve the style value. 我已经能够在DOM中看到这一点并来回切换可见性,但是到目前为止,还无法使用Jquery来检索样式值。 What I want to do is search for the div based on the class (since the "view-empty message" class is unique) and return the value of the style attribute. 我要做的是基于该类搜索div(因为“ view-empty message”类是唯一的)并返回style属性的值。 So in this case I would like to return either "display: block" or "block" . 因此,在这种情况下,我想返回"display: block""block" I've tried many different methods (such as .attr() and css. that I've seen around online ) and so far none have returned what I'm looking for. 我尝试了许多不同的方法(例如在网上看到的.attr()和CSS),但到目前为止,没有任何方法可以返回我想要的东西。 This is the latest one I'm using: 这是我正在使用的最新版本:

var displayValue = document.getElementsByClassName($(".view-empty message"))[0].style;

Any ideas? 有任何想法吗? I appreciate the help. 感谢您的帮助。

Oh sh*t. Lightbulb. 灯泡。 Your selector is wrong. 您的选择器有误。 Change: 更改:

$('.view-empty message').attr('style');

to

$('.view-empty.message').attr('style');

In your selector you're trying to get .view-empty message which looks for an HTML element named message inside of a container with a class of .view-empty . 在您的选择器中,您尝试获取.view-empty message ,该消息在具有.view-empty类的容器内查找名为message的HTML元素。 Instead, you need to specify a selector for an element that has TWO CLASSES $('.view-empty.message'); 相反,您需要为具有两个类$('.view-empty.message');的元素指定选择器$('.view-empty.message');

;] ;]


-- Helpful but irrelevant now... -- -很有帮助,但现在不相关了…-

You need to specify which attribute you are attempting to get the value for. 您需要指定要为其获取值的属性。 Notice how I'm specifying the 'style' attribute in the .attr() method. 注意我如何在.attr()方法中指定'style'属性。 This will retrieve the value of that attribute. 这将检索该属性的值。

$('.view-empty.message').attr('style');

Per the documentation for the .attr() method, there isn't actually a method that takes no parameters. 根据.attr()方法的文档,实际上没有一个不带参数的方法。 http://api.jquery.com/attr/ . http://api.jquery.com/attr/

When you invoke the method with one value, it returns the value of the attribute. 当您使用一个值调用该方法时,它将返回属性的值。

<div class='foo'></div>

$('.foo').attr('class'); // returns "foo"

When you invoke the method with two values, it sets an attribute on the element with the name of the first argument and a value equal to the second argument. 当您使用两个值调用该方法时,它将在元素上设置一个属性,该属性具有第一个参数的名称和一个等于第二个参数的值。

<div class='foo'></div>

$('.foo').attr('style', 'display: none;');

<div class='foo' style='display: none;'></div>

perhaps try $(".view-empty message").css('display') ? 也许尝试$(".view-empty message").css('display')吗? you can also set the display property with $(".view-empty message").css('display', 'none'). 您还可以使用$(".view-empty message").css('display', 'none').设置display属性$(".view-empty message").css('display', 'none').

I assume you are not using the style='display:none' as a flag/state variable of some sort. 我假设您没有使用style='display:none'作为某种标志/状态变量。 if you are, maybe try putting the display property in a class and use jquery's .addClass() and .removeClass() ? 如果是的话,也许尝试将display属性放在一个类中,并使用jquery的.addClass().removeClass()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM