单击关闭菜单/ div以关闭

时间:2015-04-02 12:08:59

标签: javascript jquery

当我点击页面上的任何地方时,我一直试图关闭我的菜单,除了在菜单中。

我设法实现了这个目的,当菜单中的链接被点击时,通过给它提供与菜单按钮相同的onclick功能,但我没有成功点击菜单关闭它。

http://codepen.io/anon/pen/LEvdmW

JS

function setVisibility(id) {
  var targetButton;
  switch( id ) {
    case "layer":
      targetButton = "button";
      break;
  }
  if (document.getElementById(targetButton).value == 'Close') {
    document.getElementById(targetButton).innerHTML = 'Open';
    document.getElementById(targetButton).value = 'Open';
    document.getElementById(id).style.display = 'none';
  } else {
    document.getElementById(targetButton).innerHTML = 'Close';
    document.getElementById(targetButton).value = 'Close';
    document.getElementById(id).style.display = 'inline';
  }
}

HTML

<button name="type" id="button" onclick="setVisibility('layer')">Open</button>
<div id="layer"><a onclick="setVisibility('layer')"> Hello</div>

CSS

#layer {
    position: absolute;
    left: 8px;
    top: 50px;
    background-color: #989898;
    width: 280px;
    height: 100px;
    padding: 10px;
    color: black;
    display: none;
}


button {  border:none; background:#989898; color:#fff; padding:10px; outline:none; cursor:pointer;

}

2 个答案:

答案 0 :(得分:5)

可以通过以下代码实现。

$(document).mouseup(function (e)
{
    var container = $("YOUR CONTAINER SELECTOR");

    if (!container.is(e.target) // if the target of the click isn't the container...
        && container.has(e.target).length === 0) // ... nor a descendant of the container
    {
        container.hide();
    }
});

工作小提琴:http://jsfiddle.net/LCB5W/153/

更新小提琴:http://jsfiddle.net/LCB5W/154/

答案 1 :(得分:0)

你可以在div后面有一个背景,你有onclick事件来隐藏div。检查下面的修改代码(修改代码以便您理解):

工作示例:DEMO HERE

HTML

<button name="type" id="button" onclick="setVisibility('layer')">Open</button>
<div id="backdrop" onclick="setVisibility('layer')"></div>
<div id="layer">Hello</div>

CSS

#layer {
    position: absolute;
    left: 8px;
    top: 50px;
    background-color: white;
    width: 280px;
    height: 100px;
    padding: 10px;
    color: black;
    display: none;
    z-index : 999;
}

#backdrop {
    position : absolute;
    top : 0;
    left : 0;
    width : 100%;
    height : 100%;
    background : black;
    opacity : 0.5;
    z-index : 2;
    display : none;
}

button {  
    border:none; 
    background:#989898; 
    color:#fff; 
    padding:10px; 
    outline:none; 
    cursor:pointer;
}

JS

function setVisibility(id) {
  var targetButton;
  switch( id ) {
    case "layer":
      targetButton = "button";
      break;
  }
  if (document.getElementById(targetButton).value == 'Close') {

    document.getElementById(targetButton).innerHTML = 'Open';
    document.getElementById(targetButton).value = 'Open';
    document.getElementById(id).style.display = 'none';
    document.getElementById("backdrop").style.display = "none"; 

  } else {

    document.getElementById("backdrop").style.display = "block"; 
    document.getElementById(targetButton).innerHTML = 'Close';
    document.getElementById(targetButton).value = 'Close';
    document.getElementById(id).style.display = 'inline';

  }
}