简体   繁体   English

如何检索CSS中定义的背景图片网址?

[英]How to retrieve the background image url defined in CSS?

Suppose I have a div 假设我有一个div
<div id="div_a" style="background-image:url('xxx')"></div>

Then I can get the url info by using 然后我可以通过使用获取URL信息

document.getElementById("div_a").style.backgroundImage  

to get its url info. 以获得其网址信息。

However if the style is defined in CSS the code above would return blank 但是,如果样式是在CSS中定义的,则上面的代码将返回空白

Anything wrong with it? 有什么问题吗?

您正在寻找的是计算样式getComputedStyle

window.getComputedStyle(document.getElementById("div_a"), null).getPropertyValue('background-image')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM