2018-08-21

作者: 轩爱青 | 来源:发表于2018-08-21 09:41 被阅读0次

    内容详情图片地址转换
    var str = '<div><img a="b" alt="123" src="/news/img/01.jpg" width=400 height=400><img src="http://www.otherdomain.com/news/img/02.jpg" width=400 height=400><img src="http://www.domain.com/news/img/03.jpg" width=400 height=400><img src="/news/img/04.jpg" width=400 height=400></div>';
    var reg = /<img(.*?)src="([^"]+)"/gi;
    var newUrl;
    var domain = location.protocol + '//' + location.hostname +'/';
    str = str.replace(reg, function(img, attrs, src) {
    newUrl = 'src="' + src;
    if(!((src.indexOf('http://') == 0) || (src.indexOf('https://') == 0))) {
    newUrl = 'src="' + domain + src;
    }
    return '<img' + attrs + newUrl + '"';
    })
    return str;

    相关文章

      网友评论

        本文标题:2018-08-21

        本文链接:https://www.haomeiwen.com/subject/cjldiftx.html