我正在學習jquery,我有選擇器這個小問題。jQuery上一級和.find()
這是我的DOM結構:
<li>
<header class="title">
<span>Text</span>
<a href="#work-1">My trigger</a>
</header>
<div id="work-1">
<div class="description">
<p>some shit about the work</p>
</div>
<div class="carousel">
<img/>
<img/>
<img/>
...
</div>
</div>
</li>
確定。它是一個簡單的列表,與我的作品有很多聯繫。每個項目都有其描述和一些照片,在旋轉木馬上進行。
當我點擊鏈接,我想在jquery中創建一個變量來獲得傳送帶。我寫這個,但它不工作:
$('a').click(function(e){
var href = $(e.target).attr('href');
// this is to make my div#work toggle from display:none to block.
var carousel = $(href).find('.carousel');
// this is the wrong code. I cant reach the carousel from there.
});
感謝您的幫助!
使用'parent()'去1級別 –
我試過你的代碼,它的工作原理。你的方式也可以說比這裏的答案更好,所以我會堅持下去。你確定你的例子中的HTML與你的頁面結構相匹配嗎? – dlsso