Switching an image using jQuery

后端 未结 7 1834
醉酒成梦
醉酒成梦 2021-02-01 23:29

Is there a better, more jQuery-ish way of handling this image substitution?

var image = $(obj).children(\"img\");
if ($(image).attr(\"src\") == \"Images/TreeColl         


        
相关标签:
7条回答
  • 2021-02-02 00:35

    You could do something like this

    e.g

    $(function()
        {
           $(obj)
           .children("img")
           .attr('src', swapImage );    
        });
    
    function swapImage(){
        return ( 
                  $(this).attr('src') == "Images/TreeCollapse.gif" ?
                                         "Images/TreeExpand.gif" :
                                         "Images/TreeCollapse.gif");
    }
    

    N.B in your question you do $(image) multiple times. Its better to cache the lookup in a var e.g var $image=$(obj).children("img"); then use the $image from there on in.

    0 讨论(0)
提交回复
热议问题