Switching an image using jQuery

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

    Wow. Answers come flying in, don't they? All of the above would work, but you could try this for a one-liner (it's untested)...

    image.setAttribute("src", "Images/Tree" + ((image.getAttribute("src").indexOf("Collapse")>0) ? "Expand" : "Collapse") + ".gif");
    

    Update: I've just tested this and it works, so would the person who voted it down care to explain why they did that?

提交回复
热议问题