Jquery - 如何使用类和属性查找元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2148523/
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
Jquery - How to find an element using class and attribute
提问by Murtaza Mandvi
I am trying to figure out the most efficient way to find my element. Following i smy structure:
我试图找出最有效的方法来找到我的元素。遵循我的结构:
<div class="a" customattrib="2">
in order to find this element can I do something like :
为了找到这个元素,我可以做类似的事情:
$("div.a [customattrib='2']")
This does not seem to work, is there another way to do this?
这似乎不起作用,有没有其他方法可以做到这一点?
Without the class I am able to get the value but I do not think this is efficient enough for my structure:
没有课程,我可以获得价值,但我认为这对我的结构来说不够有效:
$("div [customattrib='2']")
回答by SLaks
Remove the space:
删除空格:
$("div.a[customattrib='2']")
By putting in the space, you're making it into a descendant selector which finds all elements that match [customattrib='2']
and are insidean element that matches div.a
.
通过放入空格,您将其变成了一个后代选择器,它会查找所有匹配的元素[customattrib='2']
并位于匹配的元素内div.a
。