javascript 从 background-image 属性获取 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6397529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get URL from background-image Property
提问by Ozzy
i am currently using this to get the url from the background-image property:
我目前正在使用它从 background-image 属性中获取 url:
var url = $(this).find('div').css('background-image');
url = url.substr(4, url.length - 5);
This works fine however in some browser (IE6-9), instead of it being:
然而,这在某些浏览器(IE6-9)中工作正常,而不是:
url(http://.com/)
its
它的
url("http://.com/)
Is there a failsafe way that will just get the url from this property? without having to do browser detection or some other stuff?
是否有一种故障安全方法可以从该属性中获取 url?无需进行浏览器检测或其他一些东西?
回答by Felix Kling
You could do:
你可以这样做:
url = url.replace(/^url\(["']?/, '').replace(/["']?\)$/, '');
This will remove url('
and url("
from the beginning of the string if it is present and ")
resp. ')
from the end.
如果它存在并且resp,这将从字符串的开头删除url('
和。从最后。url("
")
')