打开/关闭图标

Chr*_*len 1 html javascript jquery dom

我正在用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>
Run Code Online (Sandbox Code Playgroud)

$('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');
            });
Run Code Online (Sandbox Code Playgroud)

Dou*_*yle 5

$(this).contents('img.expander')
Run Code Online (Sandbox Code Playgroud)

这就是你想要的.它将选择列表中所有子节点.在您的情况下,您的所有图像都嵌套在list元素中,因此这将仅过滤掉您想要的内容.