Jquery 样式检查和查找元素
在我的脚本中,我正在寻找具有类 .se
并具有 style
属性 display: block
的 DIV
,我需要 DIV
的 ID
。
我的方法是错误的:(
var usedse = $(".se"):has(css('display', 'block')).attr("id");
你能帮我解决这个问题吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我推荐这个:
I'd recommend this:
您可以使用
.filter()
编写大量自定义逻辑。这将找到所有并检查它们的 CSS
display
属性是否设置为block
,返回第一个匹配元素的id
。You can write a lot of custom logic using
.filter()
... This will find all<div class='se'>
and check that their CSSdisplay
property is set toblock
, returning theid
of the first matched element.您使用的语法是错误的,请尝试以下简单的方法:
The syntax you are using is wrong, try this simple way instead:
你有几个选择。
您可以做的一件事是属性选择器,它将返回具有类
se
和display
css 属性集的所有元素:或者,您可以使用
.css()
方法并实际检查某个 css 属性的值:我可以问你为什么要这样做吗?我觉得必须有更好的方法来看待这个问题......也许你应该简单地在这个元素上放置一个类(显然,这可能是不可能的,具体取决于具体的问题)?
You have a few options.
One thing you could do is the
attribute selector
which would return all elements with classse
anddisplay
css property set:Or, you could use the
.css()
method and actually check the value of a certain css property:Can I ask why you are doing this though? I feel like there must be a better way to look at the problem... maybe you should simply put a class on this element (obviously, this may not be possible depending on the exact problem)?
这可能是实现此目的的最佳方法:)
但是您必须在这里考虑多个,因为您使用类作为选择器通常意味着您试图获取多个 div 元素,如果是这种情况,请尝试这种方式
只是一个旁注:
您必须确保您的选择器在涉及 css 时选择正确的项目,例如
style = $('element').attr('style');
将仅返回值形成实际元素,例如要从 css 中设置的元素中获取样式值,您必须使用选择器将它们过滤掉。
http://jsfiddle.net/4jVC8/
您需要确保记住它们是分开的!
这是另一个过滤器版本。
This probably be the best way to accomplish this :)
But you would have to think of multiples here, as your using the classes as selectors it usually means theres multiple div elements that your trying to grab, if thats the case, try this way instead
Just a side note:
You have to make sure that your selectors are selecting the correct item when it comes down to css, For example
style = $('element').attr('style');
will only return the value form the actual element such as<div style="display:block"
.To get the style values from the element that have been set within css you will have to use the selector to filter them out.
http://jsfiddle.net/4jVC8/
You need to make sure you keep in mind that these are separate!
So heres another filter version.