給button添加onclick屬性時(shí),怎么函數(shù)直接就出來(lái)了?如何再次設(shè)置為點(diǎn)擊之后觸發(fā)函數(shù)?
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<script type="text/javascript">
? ? var main = document.body;
? ? var p1 = document.createElement("p");
? ? var p2 = document.createElement("p");
? ? var br = document.createElement("br");
? ? var inp1 = document.createElement("input");
? ? var inp2 = document.createElement("input");
? ? var inp3 = document.createElement("input");
? ? p1.innerHTML = "高度:";
? ? p2.innerHTML = "寬度:";
? ? inp1.type = "text";
? ? inp2.type = "text";
? ? inp3.type = "button";
? ? inp3.value = "顯示可視區(qū)域大小";
? ??
? ? main.appendChild(p1);
? ? main.appendChild(inp1);
? ? main.appendChild(p2);?
? ? main.appendChild(inp2);
? ? main.appendChild(br);
? ? main.appendChild(inp3);
? ? inp3.setAttribute("onclick",test())
? ??
? ? function test(){
? ? ?alert("test!") ??
? ? }
? ? /*function getv(){
? ? var w =document.documentElement.clientWidth||document.body.clientHeight;
? ? var h = document.documentElement.clientHeight||document.body.clientHeight;
? ? inp1.value = w;
? ? inp2.value = h;
? ? //document.write(w)
? ? }*/
</script>
</body>
</html>
2015-10-05
inp3.setAttribute("onclick","test()");
代碼改成這樣就可以了。