我有一个显示的浮动 div,我希望当用户单击该 div 时将其隐藏。这类似于悬停在元素上时的 .hover() 函数回调。只有我想这样做才能点击。

我尝试为 body 设置一个单击事件,这会隐藏 div,但这给出了意想不到的结果。

有人知道我如何轻松做到这一点吗?

有帮助吗?

解决方案

另一个可能更简单的选择是在浮动DIV和页面其余部分之间添加透明div。

透明DIV上的简单点击事件可以处理隐藏,它可以避免您遇到点击事件时遇到的问题。

其他提示

如果要在单击页面中的其他位置时清除div,可以执行以下操作:

$('body').click(function(event) {
    if (!$(event.target).closest('#myDiv').length) {
        $('#myDiv').hide();
    };
});

如果你正在使用Jquery,你可以使用像:

这样的选择器
$("*:not(#myDiv)").live("click", function(){
    $("#myDiv").hide();
});

当然,您正在寻找模糊活动吗?

最好的方法是: -

    
$(document).bind('click', function(e) {  

 var $clicked = $(e.target);

    if (!$clicked.parents().hasClass("divtohide")) {
        $(".divtohide").hide();
    }

});

这对我有用,

var mouseOver = false;
$("#divToHide").mouseover(function(){mouseOver=true;});
$("#divToHide").mouseout(function(){mouseOver=false;});
$("body").click(function(){
      if(mouseOver == false) {
           $("#divToHide").hide();
      }
});

示例单击链接元素以显示div菜单,您只需将模糊功能绑定到链接元素以隐藏div菜单

$('a#displaymenu').click(function(){
   $("#divName").toggle();
}).blur(function() {$("#divName").hide()})

这是一个处理clickout事件的函数,我将它提供给弹出窗口的选择器和jquery元素。可能更适合作为jquery插件,但这很简单。

clickOut = function(selector, element) {
 var hide = function(event) {
  // Hide search options if clicked out
  if (!$(event.originalEvent.target).parents(selector).size())
   $(element).hide();
  else
   $(document).one("click",hide);
 };

 $(document).one("click", hide);
};

因此,如果你有像<div class='popup'>test</div>这样的弹出元素,你可以使用我的函数,如clickOut("div.popup", $("div.popup"));

     $('body').click(function (event) {        
if ($("#divID").is(':visible')) {
            $('#divID').slideUp();
        }
});

这可用于检查div是否可见,如果可见,则会将对象向上滑动。

这是一个成熟的事件驱动方法

  • 自定义事件处理层的“召唤”和“解散”,以免踩到其他基于点击的事件
  • 仅当相关图层实际可见时,document.body 才会侦听关闭事件

泽代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script type="text/javascript">

$(function()
{
  var $layer = $('#layer');
  var $body  = $('html');

  $layer
    .bind( 'summon', function( e )
    {
      $layer.show();
      $body.bind( 'click', dismissLayer );
    } )
    .bind( 'dismiss', function( e )
    {
      $layer.hide();
      $body.unbind( 'click', dismissLayer );
    } )
    .click( function( e )
    {
      e.stopPropagation();
    })
    .trigger( 'dismiss' )
  ;

  function dismissLayer( e )
  {
    $layer.trigger( 'dismiss' );
  }

  // This is optional - this just triggers the div to 'visible'
  $('#control').click( function( e )
  {
    var $layer = $('#layer:hidden');
    if ( $layer.length )
    {
      $layer.trigger( 'summon' );
      e.stopPropagation();
    }
  } );
});

</script>

<style type="text/css">
#layer {
  position: absolute;
  left: 100px;
  top: 20px;
  background-color: red;
  padding: 10px;
  color: white;
}
#control {
  cursor: pointer;
}
</style>

</head>
<body>

<div id="layer">test</div>
<span id="control">Show div</span>

</body>
</html>

我知道很多代码,但这里只是为了展示不同的方法。

在文档上使用事件处理程序对我很有用:

function popUp( element )
{
    element.onmousedown = function (event) { event.stopPropagation(); };
    document.onmousedown = function () { popDown( element ); };

    document.body.appendChild( element );
}

function popDown( element )
{
    document.body.removeChild( element );

    document.onmousedown = null;
}

我在一个论坛中找到了解决方案......但是我找不到它归功于原始作者。 这是版本(修改后的代码)。

 $(document).bind('mousedown.yourstuff', function(e) {
            var clicked=$(e.target); // get the element clicked                 
            if( clicked.is('#yourstuff')
                 || clicked.parents().is('#yourstuff')) {
                // click safe!
            } else {
                // outside click
                closeIt();
            }
        });

 function closeIt() {
        $(document).unbind('mousedown.emailSignin');
        //...
}

我还有ESC键盘绑定和上面没有图示的“关闭”html锚点。

如果您不想通过单击自己隐藏要显示的元素:

var div_active, the_div;

the_div = $("#the-div");
div_active = false;

$("#show-the-div-button").click(function() {
  if (div_active) {
    the_div.fadeOut(function(){
      div_active = false;
    });
  } else {
    the_div.fadeIn(function(){
      div_active = true;
    });
  }
});

$("body").click(function() {
  if div_active {
    the_div.fadeOut();
    div_active = false;
  }
});

the_div.click(function() {
  return false;
});

您需要监控整个页面的mouseDown事件,但是当用户在您的浮动div中单击时,您必须注意。

我建议将悬停事件添加到你的浮动div中,这样当用户将鼠标悬停在它上面时,<=>会被忽略,但是当它没有悬停在<=>上时会关闭它

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top