[Edge]。→、
NonElementParentNode 内の指定の id 名に一致する要素を Element で返します。
NonElementParentNode | Document。 |
---|---|
id | ID を表す文字列。 |
Element オブジェクト、または null。
HTML:
<html> <head> <title>getElementById example</title> </head> <body> <p id="para">Some text here</p> <button onclick="changeColor('blue');">blue</button> <button onclick="changeColor('red');">red</button> </body> </html>
JavaScript:
function changeColor(newColor){ var elem = document.getElementById('para'); elem.style.color = newColor; }
¶以下省く
HTML:
<SPAN id=IdTest>SPAN要素</SPAN>
JavaScript:
D=document.getElementById("IdTest") //=[object HTMLSpanElement]
HTML:
<SPAN id=Idあ>SPAN要素</SPAN>
JavaScript:
D=document.getElementById("Idあ"); //=[object HTMLSpanElement]
英数以外のid名でも取得が可能で、アンカーなどに利用できる。
Idあ.innerHTML="てすと";
と、なる。