我有三個格箱Click事件儘管點擊處理程序使用addEventListener(事件冒泡被禁用))
對父母設定的第一個div主要有"main"
ID和包含其他兩個箱用ID "menu_header"
和"menu_options"
這是我的HTML
<div id="main">
<div id="menu_header">
</div>
<div id="menu_options">
</div>
</div>
<span>Something here</span>
這是我的CSS
#main{
display:table-cell;
position:relative;
top:100px;
border:solid purple;
}
#menu_header{
position:relative;
border:solid black;
background:red;
width:100px;
top:200px;
height:100px;
/* -moz-transition-property:bottom;
-moz-transition-duration:2s;
transition-property:top;
transition-duration:1s;*/
}
#menu_options{
background:blue;
position:absolute;
bottom:0%;
border:solid black;
width:100px;
height:100px;
}
/*#menu_header:active{
top:100px;
}*/
(忽略CSS過渡,因爲它被註釋掉)
這是我的javascript
function test(e){
//if(e.target.id=="menu_options"){return};
$('#menu_header').animate({top:'-10'},1000);
//var t=document.getElementById('menu_header')
// t.setAttribute('top','40px');
t.style="position:relative;border:solid black;background:red;width:100px;top:60px;height:100px;"
}
document.getElementById('main').addEventListener('click',test,false)
終於這是我的實驗室老鼠的jsfiddle鏈接 http://jsfiddle.net/repzeroworld/ev7k688s/
在這種情況下, n個I設置單擊事件處理我的父容器上的ID爲"main"
一切完美,只是每當點擊此父的容器中的子DIV ID爲"menu_options"
這也引發
我發現下面的鏈接,但不知何故這裏event.propogation()
解決方案的處理程序似乎不適用於我的情況。
clicking on child element also triggers click event on it parent
一個討厭的黑客,防止任何行動執行如果孩子的div捕獲的事件是
if(e.target.id=="menu_header"){return};
上面我在javascript代碼註釋掉。
問: 爲什麼id爲"menu_options"
從父容器的id "main"
捕捉事件,儘管我對設置add EventListener
false
孩子div來阻止事件冒泡?
這是'event.stopPropagation()','不event.propagation()'。用'addEventListener()'設置'false'不會禁止冒泡。 –