给button添加onclick属性时,怎么函数直接就出来了?如何再次设置为点击之后触发函数?

来源:9-18 浏览器窗口可视区域大小

Detonate

2015-09-25 10:49

<!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 = "显示可视区域大小";

    

    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>


写回答 关注

1回答

  • 雨丶辰
    2015-10-05 17:39:51

    inp3.setAttribute("onclick","test()");

    代码改成这样就可以了。

JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

468800 学习 · 22582 问题

查看课程

相似问题