我在获取 div 的背景 url 时遇到问题
HTML:
<div id='test'></div>
CSS:
#test { position:absolute; height:55px; width:85px; top:554px; left:197px; background:url('images/1.jpg'); }
当我尝试使用下面的方法获取背景图像 url 时,它显示为空白。
alert(document.getElementById("test").style.backgroundImage);
但有趣的是它适用于以下
document.getElementById("test").style.backgroundImage="url('images/1.jpg')";
alert(document.getElementById("test").style.backgroundImage);
注意:网址没有问题,因为图像显示正确
最佳答案
你无法从外部 CSS 获取样式信息,因为 JS 只查看 DOM。我不确定是否可能,但是当您通过样式标记直接在元素上设置 CSS 信息时,您应该能够获取和修改 CSS 信息:
<div style="position:absolute; height:55px; width:85px; top:554px; left:197px; background:url('images/1.jpg');"></div>
关于javascript - javascript的背景图像问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2154181/