虽然有很多好的资源用jQuery遍历DOM,但我没有任何运气对我的例子。点击jquery替代元素的链接
在我的网站www.smithgrey.co.uk我有一系列的缩略图画廊,在他们面前都有一个“栏目标题”div。
我想实现的是当您点击'section-title'div时,点击右边缩略图的第一个<a href>
链接。
class="section-title"
有多个实例 - 不幸的是,我无法添加任何ID或类,因为它是动态生成的,并且被要求尽可能保持原样。
的HTML看起来像这样:
<body>
<div id="content-grid">
<div class="section-title">
<span class="section-title-type">Title of the first thumbnail gallery</span>
</div>
<div class="index-article">
<a href="#" class="fancybox">
<img src="thumbnail-1">
</a>
</div>
<div class="section-title">
<span class="section-title-type">Title of the second thumbnail gallery</span>
</div>
<div class="index-article">
<a href="#" class="fancybox">
<img src="thumbnail-2">
</a>
</div>
<div class="index-article">
<a href="#" class="fancybox">
<img src="thumbnail-3">
</a>
</div>
<div class="index-article">
<a href="#" class="fancybox">
<img src="thumbnail-4">
</a>
</div>
</div>
</body>
这里是我的jQuery,到目前为止,但它并不能产生结果,我会想象,因为它会点击链接不管我点击第一或第二<div class="section-title">
。
$('div.section-title').click(function(){
$(this).parent().next($('div.index-article')).children('a.fancybox').click();
});
解决方案:
随着我所管理的其他评论者的帮助下找出如何使它工作如我所料:
$(document).ready(function() {
$('div.section-title').click(function(){
$(this).next('.index-article').find('a.fancybox:first').click();
});
});
嘿,我给你的建议一去,但没有运气,但我开始用的.next(拨弄),我得到它的工作 - 所以谢谢你的帮助。 –