阻止事件冒泡 阻止浏览器的默认行为
生活随笔
收集整理的這篇文章主要介紹了
阻止事件冒泡 阻止浏览器的默认行为
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8"> <title>阻止事件冒泡 阻止瀏覽器的默認行為</title> <style type="text/css"></style>
</head>
<body><button id="btn1">按鈕1</button><input type="text" id="m-text"><script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script><script type="text/javascript">$(function() {function stopBubble(e) { //如果提供了事件對象,則這是一個非IE瀏覽器 if (e && e.stopPropagation) {//因此它支持W3C的stopPropagation()方法 e.stopPropagation(); } else {//否則,我們需要使用IE的方式來取消事件冒泡 window.event.cancelBubble = true; }} $('body').on('click', function() {console.log('body');});$('#btn1').on('click', function(e) {console.log('按鈕1');stopBubble(e); //阻止冒泡});//阻止瀏覽器的默認行為 function stopDefault(e) { //阻止默認瀏覽器動作(W3C) if (e && e.preventDefault) {e.preventDefault(); } else { //IE中阻止函數器默認動作的方式 window.event.returnValue = false; }return false; } $('#m-text').on('keydown', function(e) {console.log(String.fromCharCode(e.keyCode));stopDefault(e);});});</script>
</body>
</html>
總結
以上是生活随笔為你收集整理的阻止事件冒泡 阻止浏览器的默认行为的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 智慧消防物联网(智慧消防远程监控管理系统
- 下一篇: 自测过程,开发要有足够细心和敏感度