开发者

jquery :first element of a list of anchor tags

开发者 https://www.devze.com 2023-04-02 22:07 出处:网络
My jquery looks like this $(\'#content a[href*=\"Something\"]\') and returns this [<a href=\"Something1\">blah</a>,

My jquery looks like this

$('#content a[href*="Something"]')

and returns this

[<a href="Something1">blah</a>,
 <a href="Something2">blah</a>,
 <a href="Something3">blah</a>,
 <a href="Something4">blah</a>]

I'm trying to use :first to get the first element.

$('#content a[href*="Something"]:first')

But it's not working. The #content part seems to be causing a problem. How can I fix this without remo开发者_运维问答ving #content?

Edit: my html

<div id="content>
  <a href="Something1">blah</a>
  <a href="Something2">blah</a>
  <a href="Something3">blah</a>
  <a href="Something4">blah</a>
</div>


Look at this Demo


I'm not sure why :first isn't working for you, it works for me in similar situations, but if nothing else works, try using filter() instead:

$('#content a[href*="Something"]').filter(':first')


Use eq selector

$('a[href*="Something"]:eq(0)')

If you want to use the context then try this.

var context = $('#content');
var firstElement = $('a[href*="Something"]:eq(0)',context);
0

精彩评论

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