2014-02-14 24 views
0

我有一個父div,它的任何區域都有一個點擊動作。在這個父div裏面,我還有一個子div也帶有一個點擊動作。當我選擇子div時,兩個點擊事件都會觸發。如何指定子div被點擊的父母div中也有一個點擊動作

我怎樣才能讓它指定當孩子被點擊,而不是觸發父母點擊事件?

HTML

<div id="Parent"> 
    <div class="Child"></div> 
</div> 

CSS

#Parent 
{ 
    Width: 300px; 
    height: 300px; 
    background: #000; 
} 

.Child 
{ 
    Width: 100px; 
    height: 100px; 
    background: blue; 
} 

jQuery的

$("#Parent").click(function() { 
    window.open('http://www.Google.com', '_blank'); 
}); 

$(".Child").click(function() { 
    window.open('http://www.Bing.com', '_blank'); 
}); 

回答

5

這是因爲該事件「巴布減少「通過孩子的所有祖先元素。爲防止這種情況發生,請致電event.stopPropagation()

$(".Child").click(function (event) { 
    event.stopPropagation() 
    window.open('http://www.Bing.com', '_blank'); 
});