javascript来显示元素属性

时间:2015-04-06 17:09:22

标签: javascript

以下是我尝试显示元素的属性或属性。 我该如何解决这个问题?

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

2 个答案:

答案 0 :(得分:1)

首先,按ID获取元素的功能是document.getElementById("demo");

这应该指向你更好的方向......

答案 1 :(得分:1)

您需要更改

var el = getElementsById("demo");

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