雖然有很多好的資源用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(撥弄),我得到它的工作 - 所以謝謝你的幫助。 –