假设我有一个 div
<div id="div_a" style="background-image:url('xxx')"></div>
然后我可以通过使用获取 url 信息
document.getElementById("div_a").style.backgroundImage
获取其 url 信息。
但是,如果样式是在 CSS 中定义的,那么上面的代码将返回空白
有什么问题吗?
假设我有一个 div
<div id="div_a" style="background-image:url('xxx')"></div>
然后我可以通过使用获取 url 信息
document.getElementById("div_a").style.backgroundImage
获取其 url 信息。
但是,如果样式是在 CSS 中定义的,那么上面的代码将返回空白
有什么问题吗?
您正在寻找的是计算样式getComputedStyle
window.getComputedStyle(document.getElementById("div_a"), null).getPropertyValue('background-image')