【问题标题】:How do I make a div close when I click anywhere outside it?当我点击它之外的任何地方时,如何关闭 div?
【发布时间】:2011-12-15 00:59:14
【问题描述】:

我有一个调用#notify div 的函数。

当用户点击这个 div 时,该函数被调用。然后当用户再次点击同一个 div 时,该功能被隐藏(使用切换)。

我想知道如何使用这个相同的功能,而是有一个关闭点击元素,所以如果用户关闭点击,则 div 被隐藏。

简而言之 当用户单击 div(小框)时,会调用一个函数(小框下方会出现一个大框),删除大框的唯一方法是再次单击小框。我希望当用户点击大框外的任何位置时隐藏大框。

<script>
$(document).ready(function() {

var notify = $("#notify");
var notifyLink = $("#notify_link");
var result = $("#result");
var loader = $('#loader');
var count = $("#count");

notify.hide();
notify.click(function(event) {
    // Handle the click on the notify div so the document click doesn't close it
    event.stopPropagation();
});

notifyLink.click(

        function () { notify.toggle(notify.css('display') == 'none');
                loader.html('<?php echo elgg_view('ajax/loader',array('slashes' => true)); ?>');    
        result.load("<?php echo $vars['url']; ?>mod/notifications/ajax/data.php",function(){
                        loader.empty(); // remove the loading gif

                    });    
        });

notifyLink.toggle(
     function () {
    $(this).addClass("selected");
  },
  function () {
    $(this).removeClass("selected");
  } );

     count.load("<?php echo $vars['url']; ?>mod/notifications/ajax/livenum.php");
   var refreshId = setInterval(function() {
      count.load("<?php echo $vars['url']; ?>mod/notifications/ajax/livenum.php");
   }, 2500);
$.ajaxSetup({ cache: false });
});
</script>

<a href="#" id="notify_link">&nbsp;</a><span id="count"></span>


<div id="notify" style="display:none;">
<h4>Notifications</h4>
<div id="loader"></div>
    <div id="result" style="width:100%; height:100%;"></div>
    <div class="seeall"><a href="<?php echo $vars['url']; ?>mod/notifications/all.php">See All Notifications</a></div>
</div>

【问题讨论】:

  • 您也可以提供 HTML 正文吗?而且我对您的要求感到困惑,您能否详细说明“有一个关闭点击元素,因此如果用户关闭点击,则隐藏 div”?
  • 当用户点击 div(小框)时,会调用一个函数(小框下方会出现一个大框),移除大框的唯一方法是再次点击小框。当用户单击大框外的任何位置时,我希望大框隐藏。我希望这是有道理的。
  • 对代码进行了格式化,使其更具可读性。由于 jsbeautifier 没有忽略嵌入在 php 标记中的 ' 标记,因此必须经过多次传递。
  • 标记为收藏,稍后再查看。如果你自己解决了这个问题,请在我回复之前告诉我。

标签: jquery html events


【解决方案1】:

我希望当用户单击大框外的任何位置时隐藏大框。

点击#notify_link div 时,您需要做一些事情:

  • 启用单击document 以隐藏#notify div
  • 保持#notify_link div 的点击不会流到document 并立即关闭#notify div。您可以使用event.stopPropagation() 执行此操作
  • #notify_link div 被点击后禁用点击,因此它不会覆盖新的document 点击处理程序
  • document 点击处理程序中,将所有内容重置为显示#notify div 之前的状态

Here's a fiddle demonstrating this.

这是示例代码。我只是松散地基于您现有的代码,因此您必须将两个解决方案合并在一起。


编辑:

您在 cmets 中提到您在组合代码时遇到了麻烦。我没有你的 HTML/PHP 可以使用,但这是我目前所拥有的:

$(document).ready(function () {
    var notify = $("#notify");
    notify.hide();
    notify.click(function(event) {
        // Handle the click on the notify div so the document click doesn't close it
        event.stopPropagation();
    });

    var notifyLink = $("#notify_link");
    notifyLink.click(showNotification);

    function showNotification(event) {
        $(this).unbind('click', showNotification);

        $(this).addClass("selected");
        loadData();

        notify.show();

        $(document).click(hideNotification);

        // So the document doesn't immediately handle this same click event
        event.stopPropagation();
    };

    function hideNotification(event) {
        $(this).unbind('click', hideNotification);

        notify.hide();
        $(this).removeClass("selected");

        notifyLink.click(showNotification);
    }

    $("#count").load(
        "<?php echo $vars['url']; ?>mod/notifications/ajax/livenum.php"
    );
    var refreshId = setInterval(function () {
        $("#count").load(
            "<?php echo $vars['url']; ?>mod/notifications/ajax/livenum.php"
        );
    }, 2500);
    $.ajaxSetup({
        cache: false
    });
});

function loadData() {
    $('#loader').html(
        '<?php echo elgg_view('ajax/loader',array('slashes' => true)); ?>'
    );
    $("#result").load(
        "<?php echo $vars['url']; ?>mod/notifications/ajax/data.php",
        function () {
            $('#loader').empty(); // remove the loading gif
        }
    );
}

【讨论】:

  • 谢谢!这完全正确,您的示例完美运行。现在如何将它合并到我的代码中...
  • 非常感谢您的帮助,但我已经用了好几个小时了,我无法让它与我的代码很好地配合。你能告诉我我需要在你的代码中的哪里插入我的吗?再次感谢。
  • @StuartSloan:我隐藏了通知,所以你可以删除那行代码。我会将加载代码移到它自己的函数中,并在showNotification 中调用该函数。我会拆分 `notifyLink.toggle 代码并将一半放在每个函数中。其余的可以留在原地。
  • @StuartSloan:如果你能提供一些精简的example HTML(不是真实的),那么我可以帮助你完全集成代码。我需要知道所有 div 彼此之间的关系,并且我需要为每个 div/load 函数提供 some 类型的内容。你可以清理和伪造你需要的东西。
  • 我没有 HTML 我只有 php。
【解决方案2】:

你可以试试jquery选择器

$('*:not(.bigbox)').click(function() {
  //do stuff.. hide the box?
});

【讨论】:

  • 这比它需要的处理器密集程度要高得多,请查看.delegate().on()
  • 我的意思是,您的示例遍历整个 DOM,寻找没有 bigbox 类的所有内容,并将一个 evenListener 绑定到其中的 每个;这是非常不必要的。
  • @TheBlackBenzKid 它可能会起作用[如果大盒子中的每个元素都有那个类],但这是可怕的做法(即“无用”)
猜你喜欢
  • 2011-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-20
  • 2016-04-07
  • 1970-01-01
  • 2021-06-01
相关资源
最近更新 更多