div の子がクリックされ、親のクリック イベントもトリガーされるという古典的な問題があります。クリックすると展開および展開解除されるコンテナ内にボタンが設定されています。
ボタンをクリックすると、次のようになります。
- コンテナを解凍する
- コンテナの説明を隠す
2 つのクリック機能を以下に示します。
var $NotificationContainer = $("#NotificationContainer");
$NotificationContainer.append('<div class="Notification" title="'+title+'"></div>');
var $thisNotification = $NotificationContainer.children('.Notification[title='+uniqueTitle+']');
$thisNotification.append('<div class="NotificationDescription">'+uniqueDescription+'</div>');
$(".NotificationDescription").hide();
// Button used to close an expanded notification
$thisNotification.append("<div class='NotificationCloseButton'></div>");
$('.NotificationCloseButton').hide();
$thisNotification.click(function()
{
$(this).animate({height:250}, 1000);
$(this).find('.NotificationDescription').slideToggle('fast');
$(this).find('.NotificationCloseButton').slideToggle('fast');
});
$(".NotificationCloseButton").click(function()
{
$thisNotification.animate({height:50}, 1000);
$(this).find('.NotificationDescription').slideToggle('fast');
$(this).find('.NotificationCloseButton').slideToggle('fast');
});
このコードで私が見つけたのは、閉じるボタンをクリックしたときです:
- SlideToggle 説明を非表示にします
- Slide閉じるボタンを非表示に切り替えます
- コンテナーは展開されませんが、再度展開されます (コンテンツはまだ非表示になっています)。
クリックが呼び出されてい$thisNotification
ます(と思います)。
今、closeButton のクリックでevent.stopPropagation();
or を単純に使用しようとすると、非常に興味深い結果が得られます。return false;
上記の追加のいずれかで閉じるボタンをクリックすると、次のようになります。
- コンテナを展開解除します
- 説明とボタンはそのまま残り、slideToggle はまったく行われません。
stopPropogation を実装して false を返す正確な方法のコード スニペット:
$(".NotificationCloseButton").click(function(event)
{
event.stopPropagation();
$thisNotification.animate({height:50}, 1000);
$(this).find('.NotificationDescription').slideToggle('fast');
$(this).find('.NotificationCloseButton').slideToggle('fast');
});
と
$(".NotificationCloseButton").click(function()
{
$thisNotification.animate({height:50}, 1000);
$(this).find('.NotificationDescription').slideToggle('fast');
$(this).find('.NotificationCloseButton').slideToggle('fast');
return false;
});