Javascript div内的jQuery选择器锚点

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

jQuery selector anchor inside div

javascriptjqueryhtmlselector

提问by Hommer Smith

I have the following HTML code:

我有以下 HTML 代码:

<div id="mycontainer">
   <p> title </p>
   <ul>
   <li><span>**</span><a href="...">KEY</a></li>
   </ul>
   <ul>
   ...
   </ul>
</div>

How would I get the KEY value? In other words, from this div with id = "mycontainer", how would I go to get from the first ul, the content of the element?

我将如何获得 KEY 值?换句话说,从这个 id = "mycontainer" 的 div 中,我将如何从第一个 ul 中获取元素的内容?

回答by Adriano Carneiro

$("#mycontainer ul:first a").text();

here, have a fiddle: http://jsfiddle.net/8tvTt/

在这里,有一个小提琴:http: //jsfiddle.net/8tvTt/

回答by dsgriffin

Something like this will achieve what you're after:

这样的事情将实现你所追求的:

$("#mycontainer ul:first a").text();

$("#mycontainer ul:first a").text();

回答by A. Wolff

This works:

这有效:

$('#mycontainer ul:eq(0) a').text();

This works too: (more readable)

这也有效:(更具可读性)

$('#mycontainer ul:first a').text();

回答by Derek

Use this:

用这个:

$('#mycontainer a').text();

回答by crowjonah

While a lot of the existing answers will achieve what you're after, I think what you're really asking for (or at least what you really need) is some familiarity with jQuery selectorsand DOM traversing.

虽然很多现有的答案都能达到你所追求的目的,但我认为你真正要求的(或至少你真正需要的)是对jQuery 选择器DOM 遍历的一些熟悉。

A lot of people seem to be assuming that you want the inner text of the link element, which I am not sure is the case. Without providing for variations on this markup and without concern for speed of selectors, any number of answers will do what you want, including something as silly as $('a').text();since its the only ain the markup you provided. And we can only guess about the circumstances. It'd be more helpful to understand how each of the answers works (and why the unsuccessful ones don't, for that matter) and why.

很多人似乎假设您想要链接元素的内部文本,但我不确定是不是这种情况。如果不提供此标记的变体,也不考虑选择器的速度,任何数量的答案都可以满足您的要求,包括一些愚蠢的事情,$('a').text();因为它是a您提供的标记中唯一的。我们只能猜测情况。了解每个答案的工作原理(以及为什么不成功的答案不成功)以及原因会更有帮助。

Specificity of selectors are desirable, but you have to weigh the costs and benefits of what can actually be costly trips through the DOM. Will you always be getting the text content of a link? Could it possibly ever contain html? Do you actually want to get the wrapping <a>as well?

选择器的特异性是可取的,但您必须权衡实际上可能通过 DOM 进行昂贵旅行的成本和收益。你会一直得到链接的文本内容吗?它可能包含 html 吗?你真的想得到包装<a>吗?

If you just want the text content of the only ainside #mycontainer, sure:

如果您只想要 only ainside的文本内容#mycontainer,请确保:

$("#mycontainer ul:first a").text();

But $('#mycontainer').find('a').text();might be faster. (Wait, what's find()?, you ask, and why is it faster?) But what if KEY is formatted <strong>ly, and you want that to carry over to whatever you're doing with it? You might want to use .html()instead. If you want the aitself, why not:

$('#mycontainer').find('a').text();可能会更快。(等等,什么是find()?,你会问,为什么它更快?)但是如果 KEY 被格式化了<strong>,并且你希望它延续到你正在做的任何事情呢?您可能想.html()改用。如果你想要它a本身,为什么不:

$("#mycontainer ul:first a");

But what if each lihas an a?

但是如果每个人li都有一个a?

$("#mycontainer ul:first li:first a").clone();

Do you want the span, too?

你也想要跨度吗?

$("#mycontainer ul:first li:first").clone();

or

或者

$("#mycontainer ul:first li:first").html();

Do you want the wrapping <li>as well?

你也想要包装<li>吗?

$("#mycontainer ul:first li:first").clone();

Play around, read some more, and then you wont get everyone (myself included) so ravenous for a few silly, easy rep.

四处玩耍,多读一些,然后你就不会让每个人(包括我自己)对一些愚蠢的、简单的代表如此贪婪。

回答by Felipe Morales

You need read jquery selectors... but use this

你需要阅读 jquery 选择器...但使用这个

$('#mycontainer ul:first a').text();