Get URL from background-image Property

感情迁移 提交于 2019-11-26 18:24:23

问题


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);

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

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?


回答1:


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.



来源:https://stackoverflow.com/questions/6397529/get-url-from-background-image-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!