在用户事件上隐藏特定Div(即单击)

时间:2013-07-18 23:35:39

标签: javascript jquery jquery-mobile

我打开我的通知是通过点击页面顶部的铃声图像,然后点亮并正确打开 但现在我希望每当用户在通知div中单击时,通知框就会显示为相同但是每当点击外部div时它会关闭(div id是HSnotifications)

2 个答案:

答案 0 :(得分:4)

让文档听取点击事件。

$(document).on('click', function (e) {
    e.preventDefault();
    var $target = $(e.target);

    // You are checking the current clicked element is the div 
    // or any of the descendants of the notifications div
    if ($target.closest('#HSnotifications').length) {
        // Clicked inside the notifications.

        // do something
    } else if ($target.is('#showNotificationsBtn')) {
       // If bell button is clicked open the notification
        $('#HSnotifications').show();

    } else {

        // close notifications

        $('#HSnotifications').hide()
    }

});

<强> Check Fiddle

答案 1 :(得分:1)

这应该做你需要的。单击背景叠加层时,隐藏/关闭HSnotifications

<script>
$(document).ready(function() {
    $('.ui-panel-content-wrap').click(function (event) {
        if ($(event.target).hasClass('ui-panel-content-wrap')){
            $('#HSnotifications').hide();
        }
    });
});
</script>