console.log(document.querySelector(".green").style.backgroundColor);// gives an empty string as a result in console.green { width: 200px; height: 200px; background-color: green;}<!DOCTYPE html><html><head> <title>test</title> <meta charset="UTF-8"> <link rel="stylesheet" href="src/styles.css"></head><body> <div class="green"></div> <script src="src/index.js"> </script></body></html>我知道我也可以使用window.getComputedStyle(document.querySelector(".green")).backgroundColor;這個答案中提到了但我想知道這背后的原因,即為什么它給出一個空字符串。
為什么 element.style.backgroundColor 屬性給出一個空字符串作為結果?
炎炎設計
2023-09-28 09:30:42