jquery get href in children()
alert($(this).children().next().html());
returns
<a href="thelink.php?id=XXXX">click me...</a>
How do I change above statement to grab only the href?
Have tried attr('href') but can't get it to work.
(Yes, jq-noob and no time.)
//edit - code added:
<div class="row">
<div class="date">
<h2>27</h2>
<h3>dec</h3>
</div>
<h2><a href="/wpb/index.php?id=4192#4199">the title..</a></h2>
<p>the description...</p>
</div>
j开发者_高级运维q invoked by:
$("#threecol .row").click(function() {
alert($(this).children().next().href);
});
regards,
Find the attribute value using .attr('href').
EDIT: The first anchor within a H2:
alert($(this).find('h2 a:first').attr('href'));
You could also reference it via a class if you prefer and use the class in your anchor.
alert($(this).find('.myLink').attr('href'));
$(this).children().next()
is returning an element that has an <a>
inside of it.
To get that <a>
's href
, you need to select it, then call .attr('href')
.
You want to write something like
alert($(this).find('a:first').attr('href'));
In your case, you may want to write .find('h2 a')
.
You can try this:
Edited:
alert($(this).find("a").attr("href"));
精彩评论