我正在用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