Toggling an image src with jQuery

后端 未结 7 618
孤街浪徒
孤街浪徒 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:13

    $(document).ready(function(){
        $(".bulb").click(function(){
        var src = $(this).attr('src');
        var newsrc = (src=='images/dim.png') ? 'images/glow.png' : 'images/dim.png';
        $(this).attr('src', newsrc );
        });
    });
    

    Even more reducing lines is possible but avoided confusion. Basically , it gets the current state attribute source and checks and assigns required source.

提交回复
热议问题