JS按钮点击事件自动运行的问题?

如下是我学习时写的代码,但是一运行网页JS里面的那几个按钮事件就自动运行了,不知道我哪里搞错了,我在百度上找了看到按钮事件是像这样写的呀。请大能帮帮小弟,看小弟是哪里搞错了,谢谢哈。

代码:


<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8" />

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title>Page Title</title>

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" type="text/css" media="screen" href="main.css" />

    <script src="main.js"></script>

    <style>

        div{

            width: 200px;

            height: 200px;

            background: #998820;

        }

    </style>

    <script>

        window.onload = function(){

            var oBut_1=document.getElementById('obut_1');

            var oBut_2 = document.getElementById('obut_2');

            var oBut_3 = document.getElementById('obut_3');


            function Gored(odiv_qu, property, price, shebiao){

            var oDiv = document.getElementById(odiv_qu);

            oDiv.style.background = shebiao;

            oDiv.style[property] =price; 

            

              }

              alert(oBut_1);

            oBut_1.onclick = alert("sdsd");

            oBut_1.onclick=Gored('oDiv_s', 'width', '25rem', 'green');

            oBut_2.onclick=Gored('oDiv_s', 'height', '15rem', '#003320');

            oBut_3.onclick=Gored('oDiv_s',' ', ' ', '#022545');

        } 

        

    </script>

</head>

<body>

    <input id="obut_1" type="button" value="变红"  />

    <input id="obut_2" type="button" value="变绿" />

    <input id="obut_3" type="button" value="变黑"  />

    <div id="oDiv_s"></div>

</body>

</html>


慕尼黑5688855
浏览 768回答 3
3回答

肥皂起泡泡

明白了,谢谢哈

精慕HU

oBut_1.onclick = function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gored('oDiv_s', 'width', '25rem', 'green');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oBut_2.onclick = function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gored('oDiv_s', 'height', '15rem', '#003320');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oBut_3.onclick = function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Gored('oDiv_s',' ', ' ', '#022545');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

宝慕林4294392

你写的alert("sdsd");、Gored('oDiv_s', 'width', '25rem', 'green');都是函数执行语句,onclick需要绑定的是一个函数,像这样的:oBut_1.onclick = function(){alert("sdsd");}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript