图像src无法在Firefox中使用.replace()

时间:2015-06-01 13:19:00

标签: javascript html css

我有一个灯箱,它正在获取另一个div的背景图像,然后剥离url()然后将链接放在一个除了Firefox之外的所有浏览器中都有效,并且无法解决为什么会发生这种情况,在Firefox中对我来说,它是在src中添加''围绕着破坏链接的那个。“

_getImagePath: function($el) {
    var imagePath,
        spanEl = $el.find('span.js-cell-image-background'),
        imgEl = $el.find('img.cell-image__image');
    if(spanEl.length) {
      imagePath = spanEl.css('backgroundImage');
      imagePath = imagePath.replace('url(', '').replace(')', '');
    } else if(imgEl.length) {
      imagePath = imgEl.attr('src');
    }

    return imagePath;

  },

2 个答案:

答案 0 :(得分:1)

在括号中删除“”:

imagePath = imagePath.replace('url(', '')
                     .replace(')', '')
                     .replace(/^"/, '')
                     .replace(/"$/, '')

或在一个中:

imagePath = imagePath.replace(/^url\(\"?/, '').replace(/\"?\)$/, '')

如您所见,backgroundImage属性取决于浏览器。例如,我有一个网站,在3x浏览器中打开,打开控制台并输入:

$(".icon-home").css("backgroundImage")

铬:

"url(http://..url../images/home.png)"

火狐:

"url("http://..url../images/home.png")"

IE10:

"url("http://..url../images/home.png")" 

答案 1 :(得分:0)

感谢您回复自由我在发布前几分钟设法通过更改此行来解决此问题:

imagePath = imagePath.replace('url(', '').replace(')', '');

对此:

imagePath = imagePath.replace(/url\(["]*/,'').replace(/["]*\)/,'');

谢谢, 路加。