Toggling an image src with jQuery

后端 未结 7 607
孤街浪徒
孤街浪徒 2020-12-06 03:30

I wrote some code to toggle the src of an image and also to pause/resume the jQuery cycle2 plugin.

I\'m not sure why it isn\'t working and would appreciate some help

7条回答
  •  爱一瞬间的悲伤
    2020-12-06 04:25

    Try something like this...

    $('#toggle').attr('src', 
        $(this).data('inactive')
    );
    $('#toggle').attr('src', 
        $(this).data('active')
    );
    

    And you'll need data-inactive="example.com/inactive.jpg" and data-active="example.com/active.jpg" added to your image. By storing both as "data" you won't need any extra code to swap the URLs.

提交回复
热议问题