javascript来显示元素属性

javascript to display elements attributes

本文关键字:属性 元素 显示 javascript      更新时间:2023-09-26

下面是我尝试显示元素的属性。我该怎么解决这个问题?

  function showIt() {
    var el = getElementsById("demo");
    alert(el.style.color);
  }
h1 {
  color: blue;
}
<h1 id="demo">Hello World</h1>
<button onclick="showIt()">showIt</button>

首先,通过ID获取元素的函数是document.getElementById("demo");

这应该为你指明一个更好的方向。。。。

您需要更改

var el = getElementsById("demo");

var el = document.getElementById("demo");