
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
h2 {background:#ccc;}
</style>
</head>
<body>
<h2 style="font-size:40px; color:red; background-color:black;">js控制行内样式</h2>
<script type="text/javascript">
var hh2 = document.getElementsByTagName('h2')[0];
</script>
<script type="text/javascript">
var h2 = document.getElementsByTagName('h2')[0];
console.log( h2.style );
//console.log( h2.style[1] ); //不建议这么用
console.log( h2.style.color );
</script>
</body>
</html>
你在控制台,进行输出测试,就可以清除知道它们的关系:
h2是Dom对象,
style既是h2的属性,同时h2.style也是一个对象
而color是style下的属性
所以需要 h2.style.color,这样取 h2.style['color'] 也可以
补充:如何输出

打开浏览器,按F12,调浏览器调试工具。