繁体   English   中英

为什么我不能使用JavaScript访问CSS属性?

[英]Why am I not able to access CSS properties with JavaScript?

我无法使用以下代码在div -element上检索CSS属性:

 function myFunction() { console.log( document.getElementById("myDiv").style.borderTopColor ); } 
 div { border-top-width: 15px; border-top-color: green; } 
 <div id="myDiv">This is a div.</div> <button type="button" onclick="myFunction()">Return top border color</button> 

结合他人的评论和答案。 有关详细信息,请参阅这些内容。

做两个改变,使其工作

  • 添加边框样式: border-top-style: solid;
  • 修改JS代码以获取边框颜色: alert(getComputedStyle(document.getElementById("myDiv")).borderTopColor);

  <!DOCTYPE html> <html> <meta charset="UTF-8"> <head> <style> div { border-top-width: 15px; border-top-color: green; border-top-style: solid; } </style> </head> <body> <div id="myDiv">This is a div.</div> <br> <button type="button" onclick="myFunction()">Return top border color</button> <script> function myFunction() { alert(getComputedStyle(document.getElementById("myDiv")).borderTopColor); } </script> </body> </html> 

使用getComputedStyle获取属性值。除非指定了样式,否则边框也不可见。

以下代码段将以RGB格式返回颜色代码

 function myFunction(elem) { var elem1 = document.getElementById(elem); var style = window.getComputedStyle(elem1, null); var getBorderTop = style.getPropertyValue("border-top-color") console.log(getBorderTop); } 
 div { border-style: solid; border-top-width: 15px; border-top-color: green; } 
 <div id="myDiv">This is a div.</div> <br> <button type="button" onclick="myFunction('myDiv')">Return top border color</button> 

它应该是

     function myFunction() {  
   alert(window.getComputedStyle( document.getElementById("myDiv")).borderTopColor)
            }

这里

<!DOCTYPE html>
<html>
<head>
<style>
#myDiv {
    border: thick solid blue;
}
</style>
</head>
<body>

<div id="myDiv">This is a div.</div>
<br>
<button type="button" onclick="myFunction()">Change color of the top border</button>

<script>
function myFunction() {
    alert(document.getElementById("myDiv").style.borderTopColor = "red");
}
</script>

</body>
</html>

这对于更改顶部边框颜色是正确的。

每当我们在样式标签中定义样式道具时,我们都有权对其进行更改,并按照代码中的说明正确定义样式道具。

我希望你能理解上面的代码。 如果有任何疑问,你可以问我

您没有为边框设置样式。 默认情况下为空。 边框样式

或者,您也可以使用border-top: 15px solid green;

另外,要获取未内联定义的样式,还必须使用window.getComputedStyle

 div { border-top-width: 15px; border-top-color: green; border-top-style: solid; } 
 <!DOCTYPE html> <html> <meta charset="UTF-8"> <head> </head> <body> <div id="myDiv">This is a div.</div> <br> <button type="button" onclick="myFunction()">Return top border color</button> <script> function myFunction() { var elem = document.getElementById("myDiv"); var style = window.getComputedStyle(elem, null); alert(style.borderTopColor); } </script> </body> </html> 

尝试这样的事情:

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.mystyle
{
  border-top: 15px solid green;      
}
</style>
</head>
<body>
<div id="myDiv">This is a div.</div>
<br>
<button type="button" onclick="myFunction()">Return top border color</button>
<script>
function myFunction() {
       document.getElementById("myDiv").className = "mystyle"; 
}
</script>
</body>
</html>

暂无
暂无

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

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