我正在用jQuery做一些shennanigans,在我的擴展器旁邊加上一些加號/減號圖標。它類似於windows文件樹,或者firebugs代碼擴展器。打開/關閉圖標
它的工作原理,但它不夠具體。
希望這是有道理的......
$('div.toggle').hide();//hide all divs that are part of the expand/collapse
$('ul.product-info li a').toggle(function(event){
event.preventDefault();
$(this).next('div').slideToggle(200);//find the next div and sliiiide it
$('img.expander').attr('src','img/content/info-close.gif');//this is the part thats not specific enough!!!
},function(event) { // opposite here
event.preventDefault();
$(this).next('div').slideToggle(200);
$('img.expander').attr('src','img/content/info-open.gif');
});
<ul class="product-info">
<li>
<a class="img-link" href="#"><img class="expander" src="img/content/info-open.gif" alt="Click to exand this section" /> <span>How it compares to the other options</span>
</a>
<div class="toggle"><p>Content viewable when expanded!</p></div>
</li>
</ul>
有頁面上$('img.expander')
標籤加載,但我需要具體。我已經嘗試了next()功能(就像我用來查找下一個div一樣),但它表示它的未定義。我如何找到我的特定img.expander標籤?謝謝。
編輯,更新的代碼爲每道格拉斯的解決方案:
$('div.toggle').hide();
$('ul.product-info li a').toggle(function(event){
//$('#faq-copy .answer').hide();
event.preventDefault();
$(this).next('div').slideToggle(200);
$(this).contents('img.expander').attr('src','img/content/info-close.gif');
//alert('on');
},function(event) { // same here
event.preventDefault();
$(this).next('div').slideToggle(200);
$(this).contents('img.expander').attr('src','img/content/info-open.gif');
});
對不起,因爲我的標記亞當嚴密;) – 2008-11-07 16:55:36