onsubmit事件、事件传播判定函数stoppropagetion、事件使用2种方式

<body>
<form action=""id="form1">
    <input type="text" name="username">
    <input type="submit" value="提交  ">

</form>
    <script>
        var ele=document.getElementById("form1");//时间绑定的主要方法
        ele.onsubmit=function (e) {//onsubmit事件,点击就会触发此事件执行此函数
            //e(event)是事件对象,会封装信息,比如按了回车键,这个键的信息就封装存放在e中
            //alert(1234)
            //console.log("hello")
            //return false//不写return false 默认向后端发送文本框输入内容,
            // 写了return false 就停止不发到后端,文本框无法刷新
            e.preventDefault()//阻止默认事件和return false 相同用法
        }
    </script>
<body>
<!--事件传播:1和2的公共部分无法确定是谁的,通过stoppropagation限定1就是1的不是二的,不允许事件扩散-->
 1<div class="outer" onclick="fun2()">
    <div class="inner" ></div>
</div>
    <script>
      2  var ele=document.getElementsByClassName("inner")[0];
        ele.onclick=function (e) {
              alert("i am inner");
            e.stopPropagation();//通过他判定小的就是小的,不算大的

        }

     1   function fun2() {
            alert("i am outer")
        }
    </script>

 

posted @ 2018-06-20 14:40  未来的技术  阅读(474)  评论(1编辑  收藏  举报