jquery 选择类中的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2672532/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:00:40  来源:igfitidea点击:

jquery select element inside a class

jqueryjquery-selectors

提问by strike_ntheitroad

I want to select an anchor inside a div like this

我想在这样的 div 中选择一个锚点

<div class="response content">
  Disapproved Bank Mandiri<br><br>
  <p>
    <a class="showlist" href="#">Back to list?</a>
  </p>
</div>

What is the jquery to do this ?

什么是 jquery 来做到这一点?

回答by awgy

Any anchor in a div with "response" and "content" classes:

具有“响应”和“内容”类的 div 中的任何锚点:

$('.response.content a')

Or only anchors with a class of "showlist":

或者只有具有“showlist”类的锚点:

$('.response.content a.showlist')

回答by Sampson

If you require both classes on the DIV:

如果您需要两个类DIV

$("div.response.content a.showlist");

If not,

如果不,

$("div.response a.showlist");

To learn more about basic usage of jQuery Selectors, visit http://api.jquery.com/category/selectors/

要了解有关 jQuery 选择器基本用法的更多信息,请访问http://api.jquery.com/category/selectors/

回答by ScottyG

Another way you could solve this is using jQuery .find()

解决此问题的另一种方法是使用 jQuery .find()

Here is an example of using find() to select all elements in a div with the class "response content".

这是使用 find() 选择 div 中所有元素的示例,该 div 类为“响应内容”。

jQuery('.response.content').find('a');

This is also a helpful post that visits the topic of selectors vs. .find()

这也是一篇有用的帖子,访问了选择器与 .find()的主题