我的HTML:
<div id="gallery-thumbs">
<div class="thumb">
<img alt="2" src="gallery/thumbs/2.jpg" class="thumbimg hand" />
</div>
<div class="thumb">
<img alt="4" src="gallery/thumbs/4.jpg" class="thumbimg hand" />
</div>
<div class="thumb">
<img alt="100" src="gallery/thumbs/100.jpg" class="thumbimg hand" />
</div>
</div>
JavaScript的:
nextphotoid = $('#gallery-thumbs img[alt="4"]').parent().parent().next("div.thumb img").attr('alt');
这应该返回100,因为这是带有alt
的图像后下一个图像的alt='4'
属性的值,但它不起作用 - 为什么不呢?
我尝试了NextAll
等的各种组合。我已经阅读了类似的问题,但无法让我的工作 - 我显然错过了一些简单的事情。
答案 0 :(得分:3)
有一个父()太多,你在#gallery-thumbs的水平。使用next()会查找兄弟姐妹,而不是你想要的是什么。
试试这个:
nextphotoid = $('#gallery-thumbs img[alt="4"]')
.parent()
.next("div.thumb")
.find('img')
.attr('alt');
答案 1 :(得分:1)
您错误地使用了.next(),试试这个Fiddle
答案 2 :(得分:0)
$('div.thumb img[alt*=4]')
.parents('div :first')
.next('div')
.children('img')
.attr('alt');
试试这个Fiddle
我真的想知道何时或为什么你需要找到“ next('alt')”,当你已经为选择器提供一个