问答详情
源自:7-16 随机数 random()

大神看看我这代码哪里出了问题???

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Math </title>

<script type="text/javascript">

var i=Math.random()*10;

document.getElementsByTagName("button")[0].onclick=function(){

    document.getElementsByTagName("button")[0].innerHTML=round(i)

}

</script>

</head>

<body>

<button>点击</button>

</body>

</html>

为什么点击没有变化呢

提问者:gangsterJ4229053 2016-12-16 10:47

个回答

  • ThomasYao
    2016-12-16 11:05:40
    已采纳

    <!DOCTYPE html>

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Math </title>

    <script type="text/javascript">


    function func_click()
    {
        var i=Math.random()*10;
     
        var myobj = document.getElementsByTagName("button")[0];
        //var myobj = document.getElementById("button");
        myobj.innerHTML = i;
        document.write(myobj.innerHTML);
        
    }


    </script>

    </head>

    <body>

    <button id="button" onclick="func_click()">点击</button>

    </body>

    </html>

  • ThomasYao
    2016-12-16 11:08:25

    If I were you I will split such function into sortable steps...

  • a2020883119
    2016-12-16 10:59:56

    button不是名字

    <button id="xxxx" name="xxxx" class="xxxx">点击</button>

    document.getElementsByTagName("xxxx")[0].onclick=function(){