爲什麼下面的代碼爲style.backgroundImage輸出一個空字符串?我如何檢索圖像的高度和寬度,以便我可以適當地設置div的尺寸?爲什麼elem.style.backgroundImage返回空而不是CSS屬性?
<!DOCTYPE html>
<html>
<head>
<link href="test.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="myDiv" onclick="outputBGImageUrl(this)">
Click To Get BG Image Property
</div>
<script type="text/javascript">
function outputBGImageUrl(elem){
var bgImage = elem.style.backgroundImage;
console.log(bgImage);
}
</script>
</body>
</html>
CSS:
.myDiv{
background-image:url(box.jpg);
}
'elem.style'是指在元素上設置的內嵌樣式。 – Bill