How to update the source of an Image

前端 未结 3 1892
渐次进展
渐次进展 2021-01-01 06:54

I\'m using the Raphaël Javascript lib (awesome stuff for SVG rendering, by the way) and am currently trying to update the source of an image as the mouse goes over it.

相关标签:
3条回答
  • 2021-01-01 07:04
    var paper = Raphael("placeholder", 800, 600);
    var c = paper.image("apple.png", 100, 100, 600, 400);
    c.node.href.baseVal = "cherry.png"
    

    I hope, you get the idea.

    0 讨论(0)
  • 2021-01-01 07:13

    This works for me (and across all browsers):

    targetImg.attr({src: "http://newlocation/image.png"})
    
    0 讨论(0)
  • 2021-01-01 07:26

    I was using rmflow's answer until I started testing in IE8 and below which returned undefined for image.node.href.baseVal. IE8 and below did see image.node.src though so I wrote functions getImgSrc, setImgSrc so I can target all browsers.

    function getImgSrc(targetImg) {
        if (targetImg.node.src) {
            return targetImg.node.src;
        } else {
            return targetImg.node.href.baseVal;
        }
    }
    
    function setImgSrc(targetImg, newSrc) {
        if (targetImg.node.src) {
            targetImg.node.src = newSrc;
        } else {
            targetImg.node.href.baseVal = newSrc;
        }
    }
    
    0 讨论(0)
提交回复
热议问题