2015-12-12 41 views
2

我有一個問題我.fadeOut()當用戶點擊不工作「不」,我知道這是因爲div.confirm嵌入在span內,這哪裏是.fadeIn()函數被調用。.fadeOut()不工作

我正在研究一個需要此HTML層次結構的項目,但我需要「否」按鈕來確認.fadeOut()

Non-working Fiddle

我的HTML:

<span>Delete File 
<div class="confirm">Are you sure? 
    <div class="yes"> 
    Yes 
    </div> 
    <div class="no"> 
    No 
    </div> 
</div> 
</span> 

JS:

$('span').click(function() { 
    $('.confirm').fadeIn(100); 
}); 

$('.no').click(function() { 
    $('.confirm').fadeOut(100); 
}) 

回答

4

你需要使用e.stopPropagation();

$('span').click(function() { 
    $('.confirm').fadeIn(100); 
}); 

$('.no').click(function (e) { 
    e.stopPropagation(); 
    $('.confirm').fadeOut(100); 
}); 

Working Demo

而且,更清楚,如果你有一個以上的跨度,你可以使用

$('span').click(function() { 
    $(this).find('.confirm').fadeIn(100); 
}); 

$('.no').click(function (e) { 
    e.stopPropagation(); 
    $(this).closest('.confirm').fadeOut(100); 
}) 

Working Demo

+0

這偉大的工作。謝謝。我嘗試使用'stopPropagation()',但它不起作用。 「e」或「事件」的目的是什麼? – user1661677

+1

@ user1661677 e意味着事件..我提供我的問題與文檔https://api.jquery.com/event.stoppropagation/ –

3

一個簡單的解決方案是移動.confirm格的跨度之外 - 它應該避免這次跨度事件的發射並且仍然工作得很好。

Yey-working fiddle

<span>Delete File</span> 
<div class="confirm">Are you sure? 
    <div class="yes"> 
    Yes 
    </div> 
    <div class="no"> 
    No 
    </div> 
</div>