Switching an image using jQuery

后端 未结 7 1853
醉酒成梦
醉酒成梦 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:19

    Your image object would already be a jQUery instance so there is no need for you to pass it through $(...) again.

    A good practice is to prepend variables that are jquery instances with $ and use them directly thereafter.

    var $image = $(obj).children("img");
    if ($image.attr("src") == "Images/TreeCollapse.gif")
       $image.attr("src", "Images/TreeExpand.gif");
    else
       $image.attr("src", "Images/TreeCollapse.gif");
    

提交回复
热议问题