最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery selector anchor inside div - Stack Overflow

programmeradmin7浏览0评论

I have the following HTML code:

<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?

I have the following HTML code:

<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?

Share Improve this question edited Dec 18, 2013 at 11:07 BenMorel 36.5k51 gold badges204 silver badges335 bronze badges asked Nov 20, 2012 at 17:23 Hommer SmithHommer Smith 27.9k62 gold badges176 silver badges307 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 10
$("#mycontainer ul:first a").text();

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

Something like this will achieve what you're after:

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

This works:

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

This works too: (more readable)

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

Use this:

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

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 selectors and DOM traversing.

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 a in 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.

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?

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

$("#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 a itself, why not:

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

But what if each li has an 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?

$("#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.

You need read jquery selectors... but use this

$('#mycontainer ul:first a').text();
发布评论

评论列表(0)

  1. 暂无评论