如何在jQuery中获取具有特定类名的<span>元素?

spy*_*der 4 html jquery html5 class

使用jQuery 1.9.1和HTML5,我想捕获只有特定类名的元素.

假设我有以下HTML代码:

<div>
     <span class="req">A</span>
     <span class="req notreq">B</span>
     <span class="req notreq">C</span>
     <span class="req">D</span>
</div>
Run Code Online (Sandbox Code Playgroud)

我想只捕获<span>具有类的元素,req即值A和D.

使用jQuery,我可以使用console.log($('.req'));所有notreq值和所有值来捕获console.log($('span.req.notreq'))

我只需要req价值观.有帮助吗?

Arc*_*her 8

只需将类名添加到选择器中,就像这样......

$("span[class='req']");
Run Code Online (Sandbox Code Playgroud)

这只会将span元素req作为一个类返回.

  • 它将查找确切的字符串,而不是多个类的列表。要使该示例起作用,它必须是`&lt;span class="req anotherreq"&gt;&lt;/span&gt;`,而不是`&lt;span class="anotherreq req"&gt;&lt;/span&gt;`。它正在检查“类名等于” (2认同)