单击以关闭菜单-多个实例

时间:2018-09-11 14:30:57

标签: javascript html css

我可以在同一页面上有两个实例吗?我可以复制代码并更改类名以使其匹配吗?我已经尝试过,但无法正常工作。

我可以打开这个菜单,但是当我添加脚本来检查菜单外部的单击时,它只在个人资料头像下方的一小部分起作用。

几乎就像我只能单击主体div内未被div使用的区域。

/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// ****REMOVE SCRIPT BELOW  AND IM ABLE TO OPEN THE MENU CLICKING ON THE PICTURE-----ADD THIS SCRIPT AND IT ONLY ALLOWS THE ME TO OPEN MENU CLICKING BELOW THE PICTURE

//*****Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.check')) {

    var dropdowns = document.getElementsByClassName("navContent");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
#userAcct {
  display: inline-block;
  float: right;
  margin-right: 15px;
  height: 40px;
  width: 50px;
}

.user-img {
  height: 30px;
  border-radius: 50%;
  margin-right: 5px;
}

.user-img img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 50%;
  padding: 0;
  margin: 0px;
}

.navContent {
  position: absolute;
  top: 50px;
  right: 0;
  background-color: #06dd2a;
  width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  display: none;
}

.navContent a {
  text-decoration: none;
  display: block;
  padding: 5px 10px;
  margin: 5px;
  color: #666666;
  font-weight: bold;
}

.navContent a:hover {
  background-color: #666666;
  color: #06dd2a;
}

.show {
  display: block;
}
<!-- user account photo and nav -->
<div id="userAcct" class="check" onclick="myFunction()">

  <div class="user-img"><img src="https://s3.amazonaws.com/uifaces/faces/twitter/adellecharles/128.jpg">
    <i class="fa fa-caret-down" aria-hidden="true"></i></div>

  <!-- link area -->
  <div id="myDropdown" class="navContent">
    <a href="index.php?action=logout">Log Out</a>
  </div>

</div>

2 个答案:

答案 0 :(得分:0)

您缺少event.stopPropagation();在myFunction方法中。所以你的方法就像

function myFunction (event) {
event.stopPropagation();
  document.getElementById("myDropdown").classList.toggle("show");
}

https://jsfiddle.net/zr9mqpsk/3/

谢谢

/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
document.getElementById("userAcct").addEventListener("click", function (event) {
event.stopPropagation();
  document.getElementById("myDropdown").classList.toggle("show");
});


// ****REMOVE SCRIPT BELOW  AND IM ABLE TO OPEN THE MENU CLICKING ON THE PICTURE-----ADD THIS SCRIPT AND IT ONLY ALLOWS THE ME TO OPEN MENU CLICKING BELOW THE PICTURE

//*****Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.check')) {

    var dropdowns = document.getElementsByClassName("navContent");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
#userAcct {
  display: inline-block;
  float: right;
  margin-right: 15px;
  height: 40px;
  width: 50px;
}

.user-img {
  height: 30px;
  border-radius: 50%;
  margin-right: 5px;
}

.user-img img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 50%;
  padding: 0;
  margin: 0px;
}

.navContent {
  position: absolute;
  top: 50px;
  right: 0;
  background-color: #06dd2a;
  width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  display: none;
}

.navContent a {
  text-decoration: none;
  display: block;
  padding: 5px 10px;
  margin: 5px;
  color: #666666;
  font-weight: bold;
}

.navContent a:hover {
  background-color: #666666;
  color: #06dd2a;
}

.show {
  display: block;
}
<!-- user account photo and nav -->
<div id="userAcct" class="check">

  <div class="user-img"><img src="https://s3.amazonaws.com/uifaces/faces/twitter/adellecharles/128.jpg">
    <i class="fa fa-caret-down" aria-hidden="true"></i></div>

  <!-- link area -->
  <div id="myDropdown" class="navContent">
    <a href="index.php?action=logout">Log Out</a>
  </div>

</div>

答案 1 :(得分:0)

问题出在event propagation之内。很好的解释can be found here。在您的示例中,当您单击图像时,显示菜单,并且事件不断冒泡,触发window.onclick,立即关闭菜单。 event.stopPropagation();可以防止这种情况。

我还使用了document.getElementsByClassName("navContent show");类的组合,因为我们实际上不需要任何菜单。我们想要一个当前正在显示且需要关闭的菜单。

/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction(event) {
  event.stopPropagation();
  document.getElementById("myDropdown").classList.toggle("show");
}

// ****REMOVE SCRIPT BELOW  AND IM ABLE TO OPEN THE MENU CLICKING ON THE PICTURE-----ADD THIS SCRIPT AND IT ONLY ALLOWS THE ME TO OPEN MENU CLICKING BELOW THE PICTURE

//*****Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {

  var dropdowns = document.getElementsByClassName("navContent show");
  var i;
  for (i = 0; i < dropdowns.length; i++) {
    var openDropdown = dropdowns[i];
    if (openDropdown.classList.contains('show')) {
      openDropdown.classList.remove('show');
    }
  }

}
#userAcct {
  display: inline-block;
  float: right;
  margin-right: 15px;
  height: 40px;
  width: 50px;
}

.user-img {
  height: 30px;
  border-radius: 50%;
  margin-right: 5px;
}

.user-img img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 50%;
  padding: 0;
  margin: 0px;
}

.navContent {
  position: absolute;
  top: 50px;
  right: 0;
  background-color: #06dd2a;
  width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  display: none;
}

.navContent a {
  text-decoration: none;
  display: block;
  padding: 5px 10px;
  margin: 5px;
  color: #666666;
  font-weight: bold;
}

.navContent a:hover {
  background-color: #666666;
  color: #06dd2a;
}

.show {
  display: block;
}
<!-- user account photo and nav -->
<div id="userAcct" class="check" onclick="myFunction(event)">

  <div class="user-img"><img src="https://s3.amazonaws.com/uifaces/faces/twitter/adellecharles/128.jpg">
    <i class="fa fa-caret-down" aria-hidden="true"></i></div>

  <!-- link area -->
  <div id="myDropdown" class="navContent">
    <a href="index.php?action=logout">Log Out</a>
  </div>

</div>