Jquery - 如何使用类和属性查找元素 [英] Jquery - How to find an element using class and attribute
本文介绍了Jquery - 如何使用类和属性查找元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试找出最有效的方法来找到我的元素.以下是我的 smy 结构:
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']")
推荐答案
去掉空格:
$("div.a[customattrib='2']")
通过放入空格,您将把它变成一个后代选择器,该选择器查找所有匹配 [customattrib='2']
并且 inside 元素的元素匹配 div.a
.
By putting in the space, you're making it into a descendant selector which finds all elements that match [customattrib='2']
and are inside an element that matches div.a
.
这篇关于Jquery - 如何使用类和属性查找元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文