javascript - 单击子元素时如何防止模态显示?

标签 javascript jquery html css

您好,当我点击触发器的子元素时,如何禁用/阻止模态显示,就像这样,我不希望在单击图像时显示模态,但希望在单击灰色背景时显示模态 我知道这样做的一种方法是使用 css 的位置属性或 html 的一些棘手的东西有没有办法用 java 脚本或 Jquery 来实现它。

实际上我正在使用一些 jquery 图片库插件来显示图像,所以当我单击图片库插件时,模型会同时打开。

谢谢!

.bg{
  height:300px;
  width:100%;
  background-color:#ccc;
}

.bg img{
  border:3px solid #fff;
 
}
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
  <div class="bg" data-toggle="modal" data-target="#exampleModal">
 
          <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTR6Ng_DGam7QaHNN_Xe7bwct88m_DYH8ROusHhCwAOqeQOkUIe"/>
      
  </div>
  
  


<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>



<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

</body>
</html>

最佳答案

您可以点击子项并 e.stopPropagation();防止事件像这样冒泡到 DOM 树

$("img ").click(function(e) {
    e.stopPropagation();
});

$("img ").click(function(e) {
    e.stopPropagation();
});
.bg{
  height:300px;
  width:100%;
  background-color:#ccc;
}

.bg img{
  border:3px solid #fff;
 
}
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
  <div class="bg" data-toggle="modal" data-target="#exampleModal">
 
          <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTR6Ng_DGam7QaHNN_Xe7bwct88m_DYH8ROusHhCwAOqeQOkUIe"/>
      
  </div>
  
  


<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>



<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

</body>
</html>

关于javascript - 单击子元素时如何防止模态显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49720121/

相关文章:

javascript - 在我的 html 页面的面板中打印出错误消息

javascript - jQuery 启用/禁用带有 SELECT 选项的显示/隐藏按钮。获取剩余的选项值

javascript - 在这种情况下如何访问javascript函数内的变量?

javascript - 在使用 jQuery 追加到列表之前检查重复的 div

php - 与 Apache/PHP/Javascript 的长期连接(异步服务器推送)?

javascript - 2 列编辑器 quilljs

javascript - 单击angular js中的链接加载html View 文件

javascript - 获取数字模数范围以使返回值位于最小范围值和最大范围值之间的正确方法是什么?

javascript - 语义 ui - 根据是否切换另一个组件设置侧边栏属性

css - HTML:如何防止带有填充和边框的跨度重叠?