Javascript 子div click事件触发父click事件

Javascript 子div click事件触发父click事件,javascript,Javascript,如果单击了子div,如何防止父click事件触发 调用子处理程序中事件对象的.stopPropagation()方法 var p = document.getElementById('parent'); var c = document.getElementById('child'); p.onclick = function () { alert('parent click'); } c.onclick = function (event) { event.stopProp

如果单击了子div,如何防止父click事件触发


调用子处理程序中
事件
对象的
.stopPropagation()
方法

var p = document.getElementById('parent');
var c = document.getElementById('child');

p.onclick = function () {
    alert('parent click');
}
c.onclick = function (event) {
    event.stopPropagation();
    alert('child click');
}

要使其与IE8及更低版本兼容,请执行以下操作:

c.onclick = function (event) {
    if (event)
        event.stopPropagation();
    else
        window.event.cancelBubble = true;
    alert('child click');
}

调用子处理程序中
事件
对象的
.stopPropagation()
方法

var p = document.getElementById('parent');
var c = document.getElementById('child');

p.onclick = function () {
    alert('parent click');
}
c.onclick = function (event) {
    event.stopPropagation();
    alert('child click');
}

要使其与IE8及更低版本兼容,请执行以下操作:

c.onclick = function (event) {
    if (event)
        event.stopPropagation();
    else
        window.event.cancelBubble = true;
    alert('child click');
}

调用子处理程序中
事件
对象的
.stopPropagation()
方法

var p = document.getElementById('parent');
var c = document.getElementById('child');

p.onclick = function () {
    alert('parent click');
}
c.onclick = function (event) {
    event.stopPropagation();
    alert('child click');
}

要使其与IE8及更低版本兼容,请执行以下操作:

c.onclick = function (event) {
    if (event)
        event.stopPropagation();
    else
        window.event.cancelBubble = true;
    alert('child click');
}

调用子处理程序中
事件
对象的
.stopPropagation()
方法

var p = document.getElementById('parent');
var c = document.getElementById('child');

p.onclick = function () {
    alert('parent click');
}
c.onclick = function (event) {
    event.stopPropagation();
    alert('child click');
}

要使其与IE8及更低版本兼容,请执行以下操作:

c.onclick = function (event) {
    if (event)
        event.stopPropagation();
    else
        window.event.cancelBubble = true;
    alert('child click');
}