2014-03-27 31 views
0

这是我的HTML代码:http://jsfiddle.net/Udxyb/417/更改图片(加号或减号)slidetoggle?

在上面的代码中我想在切换效果生效时添加图像('例如加号或减号图像')?

<table id="main_table"> 
    <thead> 
     <tr class="firstline"> 
      <th>Column1</th> 
      <th>Column2</th> 
      <th>Column3</th> 
      <th>Column4</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr style="width:1002px; background-color:green; color:white"> 
      <td colspan="" class="flip"> Section 1 </td> 
      <td colspan="" class="flip"> Section 1 </td> 
      <td colspan="" class="flip"> Section 1 </td> 
      <td colspan="" class="flip"> Section 1 </td> 
     </tr> 
    </tbody> 
    <tbody class="section"> 
     <tr> 
      <td>item 111</td> 
      <td>item 112</td> 
      <td>item 113</td> 
      <td>item 114</td> 





     </tr> 
     <tr> 
      <td>item 121</td> 
      <td>item 122</td> 
      <td>item 123</td> 
      <td>item 124</td> 
     </tr> 
     <tr> 
      <td>item 131</td> 
      <td>item 132</td> 
      <td>item 133</td> 
      <td>item 134</td> 
     </tr> 
    </tbody> 
    <tbody> 
     <tr style="background-color:green; color:white"> 
      <td colspan="" class="flip"> Section 2 </td> 
      <td colspan="" class="flip"> Section 2 </td> 
      <td colspan="" class="flip"> Section 2 </td>    <td colspan="" class="flip"> Section 2 </td> 
     </tr> 
    </tbody> 
    <tbody class="section"> 
     <tr> 
      <td>item 211</td> 
      <td>item 212</td> 
      <td>item 213</td> 
      <td>item 214</td> 
     </tr> 
     <tr> 
      <td>item 221</td> 
      <td>item 222</td> 
      <td>item 223</td> 
      <td>item 224</td> 
     </tr> 
     <tr> 
      <td>item 231</td> 
      <td>item 232</td> 
      <td>item 233</td> 
      <td>item 234</td> 
     </tr> 
    </tbody> 
    <tbody> 
     <tr style="background-color:green; color:white"> 
      <td colspan="" class="flip"> Section 3 </td> 
      <td colspan="" class="flip"> Section 3 </td> 
      <td colspan="" class="flip"> Section 3 </td> 
      <td colspan="" class="flip"> Section 3 </td> 
     </tr> 
    </tbody> 
    <tbody class="section"> 
     <tr> 
      <td>item 311</td> 
      <td>item 312</td> 
      <td>item 313</td> 
      <td>item 314</td> 
     </tr> 
     <tr> 
      <td>item 321</td> 
      <td>item 322</td> 
      <td>item 323</td> 
      <td>item 324</td> 
     </tr> 
     <tr> 
      <td>item 331</td> 
      <td>item 332</td> 
      <td>item 333</td> 
      <td>item 334</td> 
     </tr> 
    </tbody> 
</table> 

的jQuery:

<script type="text/javascript"> 
jQuery(function($) { 


    $("#polls").on("click", ".flip", function() { 

     $(this) 

      .closest('tbody') 
      .next('.section') 
      .toggle('fast'); 
    }); 
}); 
</script> 
在这个jQuery

我做编码采取切换与此相伴的效果,我想的图像改变???

+0

哪里是你+/-图像 –

回答

1

试试这个。

$('.flip').click(function() { 
    var $$ = $(this); 
     $$.closest('tbody') 
     .next('.section') 
     .toggle('fast'); 
    if(!$$.hasClass('active')){ 
     $$.parent().find('span.plus').hide() 
     .end().find('.minus').show(); 
     $$.addClass('active'); 
    } else { 
     $$.parent().find('.plus').show() 
     .end().find('.minus').hide(); 
     $$.removeClass('active'); 
    } 
}); 

Fiddle Demo

+0

如何使用的,而不是你已经使用了CSS图像??? – User2413

+0

你可以使用类名来使用图像的CSS或放在不同的图像上的文本的地方 –

+0

雅很好,它为我工作! – User2413

0

您可以使用toggleClass()和jQuery来更改元素的背景图像。例如,如果您想更改点击的背景颜色<tr>

$('.flip').click(function() { 
    $(this).parent().toggleClass('opened'); 
}); 

小提琴:http://jsfiddle.net/Udxyb/450/