请问下面这个例子中的 document.getElementById("demo")
这一行代码是做什么用的?
我知道 getElementById 是获取 id 为 demo 的元素,但是这个 id 是 <p id="demo"></p>
,那么在这段代码中 <p id="demo"></p>
到底起到了什么作用呢?
document.getElementById("age")
很明显是获取 id 为 age 的 input 元素。
function myFunction() {
var age,voteable;
age = document.getElementById("age").value;
voteable = (age < 18)? "Too young" : "Old enough";
document.getElementById("demo").innerHTML = voteable;
}
<p>Click the button to check the age.</p>
Age:<input id="age" value="18" />
<p>Old enough to vote?</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>