从background-image属性获取URL [英] Get URL from background-image Property

查看:142
本文介绍了从background-image属性获取URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用它从background-image属性获取网址:

i am currently using this to get the url from the background-image property:

var url = $(this).find('div').css('background-image');
url = url.substr(4, url.length - 5);

这在一些浏览器(IE6-9)中运行正常,而不是:

This works fine however in some browser (IE6-9), instead of it being:

url(http://.com/)

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 = url.replace(/^url\(["']?/, '').replace(/["']?\)$/, '');

这将删除 url(' url(从字符串开头,如果它存在且 resp。')从最后开始。

This will remove url(' and url(" from the beginning of the string if it is present and ") resp. ') from the end.

这篇关于从background-image属性获取URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆