on是监听的意思
阻止事件发生:1、return false 2、e.preventDefault()适用于onsubmit
outer外部 inner内部
event.stopPropagation()阻止事件向外层div传播
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <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) { // console.log("hello") alert(1234);//return false//e.preventDefault() } </script></body> </html>
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title><style>.outer{width: 300px;height: 300px;background-color: antiquewhite;}.inner{width: 100px;height: 100px;background-color: rebeccapurple;}</style> </head> <body><div class="outer" onclick="func2()"><div class="inner"></div> </div><script>var ele=document.getElementsByClassName("inner")[0];ele.onclick= function (e) {alert("I am inner!");e.stopPropagation()};function func2() {alert("I am outer!")} </script> </body> </html>