Get image src with jQuery

前端 未结 3 499
-上瘾入骨i
-上瘾入骨i 2021-02-02 09:40
\"Arnold\"

How do I get with jQuery absolute path of this image?

img.attr(\"src\") give

3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-02 09:47

    I don't know that you can get it with jQuery, but you can get it with just the native JavaScript image object.

    var getSrc = function(imgSource) {
        var img = new Image();
        img.src = imgSource;
        return img.src;
    };
    

    Just call it with x = getSrc(srcAttribute) or something similar where your parameter is the string or literal holding the src you currently have in your html/image. It will return something like http://your/site/path/to/image.jpg

    http://jsfiddle.net/BradleyStaples/cQMjQ/

提交回复
热议问题