Event (prevent default, prevent bubbling)

prevent default

e.preventDefault ()

stop bubbling

e.stoppropagation ()

The event is passed from the child to the parent, which is called event bubbling

Events are passed from parent to child, called event capture

<script>
    //IE浏览器是从 子级向父级 获取标签,称为   冒泡机制
    //其他浏览器 是 从 父级向子级 获取标签,称为  捕获机制
    //现在所有的执行顺序都是从当前标签向父级标签执行
    
    // 阻止事件的传播 / 阻止冒泡事件
    // 事件对象.stopPropagation()
    
    // 兼容问题
    //     低版本IE浏览器
    //     事件对象.cancelBubble = true;   阻止冒泡事件,阻止事件的传播
    
    // 兼容语法
    if(事件对象.stopPropagation){
        事件对象.stopPropagation();
    }else{
        事件对象.cancelBubble = true;
    }
    
    //加了stopPropagation() 可以阻止冒泡事件
    事件对象.onclick = function(e){
        e = e || window.event;
        console.log('我是inner-div触发的事件');
        if(e.stopPropagation){
            e.stopPropagation();
        }else{
            e.cancelBubble = true;
        }
    }
</script>

Guess you like

Origin blog.csdn.net/Cc200171/article/details/124971038