Javascript 使用 JQuery 选择 li 元素内的第一个锚点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8774450/
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
Selecting first anchor inside li element with JQuery
提问by sqlman
My HTML looks like this:
我的 HTML 如下所示:
<li class="li-top"><a class="top sub" href=#>Blah</a> .... </li>
What I am trying to do is to select the anchor tag, so I can change the color of the text ("Blah"). But here's the catch: I am using closest() because I am starting from a descendant of that li tag:
我想要做的是选择锚标记,这样我就可以更改文本的颜色(“废话”)。但这里有一个问题:我使用的是最近的(),因为我是从那个 li 标签的后代开始的:
$(this).closest('li.li-top');
How do I get that anchor tag from this starting point? I tried next(), each(), children(), etc. I can't get it. Thanks.
我如何从这个起点获得锚标签?我试过 next()、each()、children() 等,我无法理解。谢谢。
回答by yycroman
If you're starting from one of the children, you might try:
如果您从其中一个孩子开始,您可以尝试:
$(this).parents('li.li-top').find('a:first');
I often go this way to find 'cousins once removed' in the DOM.
我经常通过这种方式在 DOM 中找到“表亲一旦被移除”。
回答by kaz
This way probably:
这样大概:
$('li.li-top a:first')
Or:
或者:
$(this).find('li.li-top a:first')