阻止默认行为

 

  

 <script>
    // 常见事件对象的属性和方法
    // 1.返回事件类型
    var div = document.querySelector("div");
    div.addEventListener("click", fn);
    div.addEventListener("mouseover", fn);
    div.addEventListener("mouseout", fn);
    function fn(e) {
      console.log(e.type);
    }
    // 2.阻止默认样式(事件) 让链接不跳转 或者让提交按钮不提交
    var a = document.querySelector("a");
    a.addEventListener("click", function (e) {
      e.preventDefault(); //dom标准写法
    });
    // 3.传统注册方式
    a.onclick = function (e) {
      // 普通浏览器 e.preventDefault( );    方法
      //e.preventDefault( );
      // 低版本浏览器 ie678 returnValue   属性
      // e.returnValue;
      // 我们可以利用return false 也能阻止默认行为  没有兼容性的问题
      // 特点: return 后面的代码不执行而且只限于传统的注册方式
      return false;
      alert(11);
    };
  </script>

 

posted @ 2022-04-18 11:31  罗砂  阅读(72)  评论(0)    收藏  举报