根据更改访问图像源

时间:2016-05-04 09:57:39

标签: javascript

我有一个函数,我调用它来更改图像src。

function checkImage(imageSrc, callback) {
    var img = new Image();
    img.src = imageSrc;
    img.onload = function() {
        var loadImage = imageSrc.replace("_1.jpg", "_2.jpg");
        callback(loadImage);
    };
    img.onerror = function() {};
 }

如何调用它的示例:

var imageSrc = 'http://www.test.com/test_1.jpg';
if(imageSrc.indexOf('_1.jpg') {
  checkImage(imageSrc, function(result) {console.log(result);})
}

因此,如果imageSrc包含' _1'那么基本上上面调用函数checkImage。我得到了正确的图像路径,但我无法弄清楚如何将imageSrc设置为新路径。我在下面试过,但我得到了原始价值。     checkImage(imageSrc,function(result){imageSrc = result;});

0 个答案:

没有答案
相关问题