jQuery - 运行函数的选择器
我如何使用以下代码在具有 .p11button
类(也具有 display:block
)的任何 div 上运行函数?
if (VARIABLE = true) {
$('div .p11-button').css('display') == 'block'
FUNCTION HERE
}
How would I use the following code to run a function on any div with the class of .p11button
which also has display:block
?
if (VARIABLE = true) {
$('div .p11-button').css('display') == 'block'
FUNCTION HERE
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试以下操作:
在您的代码示例中,您在条件中将变量分配为 true - 这将始终评估为 true!另外,选择器中的空格意味着您将选择具有 p11-button 类的 div 的后代。
Try the following:
In your code example you were assigning variable to true in the condition - this would always evaluate to true! Also, the space in your selector means you will select descendents of divs with class p11-button.
您可以使用方括号按属性选择元素:
这是一个演示:http://jsfiddle.net/2uE4s/
这将选择具有
p11-button
类的所有div
元素,并在其样式属性中包含display:block
(仅出于示例目的,我然后更改CSS所有选定元素的color
属性,以表明您不需要.each()
)。以下是 jQuery 选择器的文档: http://api.jquery.com/category/selectors/< /a>
You can select elements by their attributes using square brackets:
Here is a demo: http://jsfiddle.net/2uE4s/
This will select all
div
elements with thep11-button
class and includedisplay:block
in their style attribute (just for example purposes I then change the CSScolor
property for all the selected elements to show that you don't need an.each()
).Here is the documentation for selectors in jQuery: http://api.jquery.com/category/selectors/
像这样的东西吗?
Something like this?