Image src不能在Firefox中使用.replace()

Image src not working in Firefox with .replace()

本文关键字:replace src 不能 Firefox Image      更新时间:2023-09-26

我有一个lightbox,它正在获取另一个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;
  },

把"和括号一样去掉:

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

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

正如你所发现的,backgroundImage属性是依赖于浏览器的。举个例子,我有一个网站,在3个浏览器中打开,打开控制台,输入:

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

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

"url("http://..url../images/home.png")"
的问世

:

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

谢谢你的回复freedom -m我在你发帖前几分钟设法解决了这个问题,只是改变了这一行:

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

:

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

谢谢,卢克。