美文网首页
事件冒泡(点击穿透)

事件冒泡(点击穿透)

作者: Otherthing | 来源:发表于2018-07-06 18:49 被阅读0次

window.onload = function () {

            var oDiv1 = document.getElementById('div1');//子

            var oDiv2 = document.getElementById('div2');//父

            oDiv1.onclick = function (ev){

                var oEvent = ev || event;

                alert("this is div1");

                //js阻止事件冒泡

                oEvent.cancelBubble = true;

                oEvent.stopPropagation();

                //js阻止链接默认行为,没有停止冒泡

                //oEvent.preventDefault();

                //return false;

            }

            oDiv2.onclick = function (ev){

                var oEvent = ev || event;

                alert("this is div2");

                oEvent.cancelBubble = true;

            }

        }

相关文章

网友评论

      本文标题:事件冒泡(点击穿透)

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