美文网首页
JavaScript动态指定

JavaScript动态指定

作者: 孜然味的啊 | 来源:发表于2020-05-29 20:38 被阅读0次

    document.getElementById('inp').onclick=function(){return clickHandler();} //获得id为inp的标签.绑定事件onclick

     myform.mybutton.onclick();  //程序触发

    表name下.input的name.的事件

    <!-- edu_15_1_2.html -->

    <!doctype html>

    <html lang="en">

    <head>

      <meta charset="UTF-8">

    <title> JavaScript中地动态指定</title>

    <style type="text/css">

    #inp{width:100px;height:40px;color:red;}

    </style>

      <script type="text/javascript">

    function clickHandler()

      {

        alert("代码触发事件,即将提交表单!");

      return true;

      }

      </script>

    </head>

    <body>

      <form name="myform" method="post" action="" >

    <input id="inp" type="button" name="mybutton" value="提交" >

      </form> 

          <script type="text/javascript">

          //向 button 元素动态分配 onclick 事件

          document.getElementById('inp').onclick=function(){return clickHandler();}     

            myform.mybutton.onclick();  //程序触发

      </script>

        </body>

    </html>

    相关文章

      网友评论

          本文标题:JavaScript动态指定

          本文链接:https://www.haomeiwen.com/subject/ysgfzhtx.html