javascript - 防止父容器内的内部内容使父容器消失

标签 javascript html

我有这个脚本,它使用切换按钮来显示或隐藏目标父容器。父类容器隐藏的正确方式有两种

第二次按下切换按钮或单击父类容器外部。所以它工作得很好但是任何时候我在里面添加任何东西

父类容器,我点击那些里面的内容,它使整个父类容器消失,我该如何防止呢?

这是我的代码

  document.addEventListener('DOMContentLoaded', function(){

document.addEventListener('click',closeContainer);

function closeContainer(obj){
     var containerVar = document.querySelector('.container');
    if(obj.target.className != 'container') {
        if (containerVar.style.display === 'flex') {
            containerVar.style.display = 'none';
        } else  if(obj.target.id == 'toggle') {
            containerVar.style.display = 'flex';
        }
    }
  }
  
});
.container{
background-color: orange;
height: 350px;
width: 350px;
margin-left: auto;
margin-right: auto;
display: none;
justify-content: center;
}

.inner-container{
 margin-top: 50px;
 background-color: red;
 height: 200px;
 width: 200px;
 display: inline-block;
}
<button id='toggle'>Toggle</button>
<div class='container'>
  <div class='inner-container'></div>
</div>

最佳答案

您需要捕获容器上的点击事件并停止传播,这样就无需检查文档事件处理程序中的目标:

document.addEventListener('DOMContentLoaded', function() {

  // if click event is inside container, then stop propagation
  const container = document.querySelector('.container');
  container.addEventListener('click', function(e) {
    e.stopPropagation();
  });

  document.addEventListener('click', closeContainer);

  function closeContainer(obj) {
    var containerVar = document.querySelector('.container');
    if (containerVar.style.display === 'flex') {
      containerVar.style.display = 'none';
    } else if (obj.target.id == 'toggle') {
      containerVar.style.display = 'flex';
    }
  }

});
.container {
  background-color: orange;
  height: 350px;
  width: 350px;
  margin-left: auto;
  margin-right: auto;
  display: none;
  justify-content: center;
}

.inner-container {
  margin-top: 50px;
  background-color: red;
  height: 200px;
  width: 200px;
  display: inline-block;
}
<button id='toggle'>Toggle</button>
<div class='container'>
  <div class='inner-container'></div>
</div>

另一种解决方案是在 document 上只有一个事件处理程序,并检查目标或其任何祖先是否具有 container 类(类似于 jquery 的 closest 方法):

document.addEventListener('DOMContentLoaded', function() {

  document.addEventListener('click', closeContainer);

  function closeContainer(obj) {
    if (closest(obj.target, '.container')) return;

    var containerVar = document.querySelector('.container');
    if (containerVar.style.display === 'flex') {
      containerVar.style.display = 'none';
    } else if (obj.target.id == 'toggle') {
      containerVar.style.display = 'flex';
    }
  }

  function closest(el, selector) {
    const matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;

    while (el) {
      if (matchesSelector.call(el, selector)) {
        return el;
      } else {
        el = el.parentElement;
      }
    }
    return null;
  }

});
.container {
  background-color: orange;
  height: 350px;
  width: 350px;
  margin-left: auto;
  margin-right: auto;
  display: none;
  justify-content: center;
}

.inner-container {
  margin-top: 50px;
  background-color: red;
  height: 200px;
  width: 200px;
  display: inline-block;
}
<button id='toggle'>Toggle</button>
<div class='container'>
  <div class='inner-container'></div>
</div>

关于javascript - 防止父容器内的内部内容使父容器消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50422483/

相关文章:

javascript - HTML 和 Javascript 执行顺序

javascript - javascript静态类继承非静态类的表现

javascript - 嵌套跨度没有拾取 onClick

html - 背景图片 - 首屏优化

java - 从 servlet 创建的 html 获取信息

javascript - 如果调整视口(viewport)大小,如何显示之前隐藏的 div?

javascript - jQuery - $(audio).on ('ended' function()) 仅在第一个元素上触发一次

javascript - 获取 HTML 标签值并分配给 PHP 变量

javascript - 向页面添加内容会弄乱背景

html - 带圆圈的智能背景 div 但如果是背景图像怎么办