开发者

Select all li with a particular <span> and get link info within

I want choose all the <li> in my html that have the <span>Google Map</span> in it and apply.

And get the value in href with jQuery. I tried going through jQuery docs but could not figure it out. Is this possible?

<li><span>Google Maps Staic Link:</span> 
<a target="_blank" href="**Value To Get**" rel="nofollow">Map</a></li>

The actual HTML will be something like this.

<li>&开发者_StackOverflowlt;span>Name</span>Newyork:</li>
<li><span>Added:</span>23rd April</li>
<li><span>Google Map Link:</span> 
<a target="_blank" href="**Value To Get**" rel="nofollow">Map</a>
</li>

From this I only want the one with Google Map Link. The link will change but the span <span>Google Map Link:</span> will not change.


$('span:contains("Google Map")').next('a').attr('href');

that will give you an jQuery array object of all the hrefs


$(function(){
   var href = $('li:has(span:contains(Google Map))').find('a').attr('href');
});​

You would need to invoke .each() if you expect more than one occurence.

Anyway, selectors like this look so wierd and odd, I'd suggest to use .filter() anyway.


​$('span:contains("Google Map")').each(function(index){
    alert($(this).siblings(0).attr('href')); //Do whatever with href attr...
});​​​​​​​​​
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜