最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to toggle two images onClick - Stack Overflow

programmeradmin7浏览0评论

I'm making a collapsible treeView. I made it all, I just need my + and - icons to toggle whenever they are clicked.

I did the part when I change an icon from + to -, on click, with jQuery with the following code:

$(this).attr('src','../images/expand.gif');

Problem is, I don't know how to make it go other way around, when i click on the node again :)

I'm making a collapsible treeView. I made it all, I just need my + and - icons to toggle whenever they are clicked.

I did the part when I change an icon from + to -, on click, with jQuery with the following code:

$(this).attr('src','../images/expand.gif');

Problem is, I don't know how to make it go other way around, when i click on the node again :)

Share Improve this question edited Nov 27, 2013 at 9:23 SteBra 4,2477 gold badges38 silver badges69 bronze badges asked Mar 12, 2013 at 18:03 user1807772user1807772 1
  • possible duplicate of jQuery toggle to change image – EdChum Commented Mar 13, 2013 at 7:16
Add a ment  | 

4 Answers 4

Reset to default 8

This should work:

<style>
.expand{
    content:url("http://site./expand.gif");
}
.collapse{
    content:url("http://site./collapse.gif");
}
</style>

<img class="expand">
<script>
//onclick code
$('img.expand').toggleClass('collapse');
</script>

Look for jquery function toggleClass :)

http://jsfiddle/Ceptu/

Html:

<div id="box">

    Hello :D

</div>

Jquery:

$("#box").click(function () {
    $(this).toggleClass("red");
});

Css:

#box {
    width: 200px;
    height: 200px;
    background-color: blue;
}

.red {
   background-color: red !important;
}

Remember that !important is realy important!!!

Lots of ways to do this :D

I wanted to do this without making classes. Inside your click event function, you could do something like this:

if($(this).attr('src') == '../images/collapse.gif')
   $(this).attr('src', '../images/expand.gif');
else
   $(this).attr('src', '../images/collapse.gif');

add plus as a default img src then define a minus-class to change the image source to minus image

$("selector_for_your_link").click(function () {
      $(this).toggleClass("minus-class");
    });
发布评论

评论列表(0)

  1. 暂无评论