javascript - jQuery/Bootstrap : Child DIV opens up Parent Modal

标签 javascript jquery html css twitter-bootstrap

所以我有嵌套的 DIV,一个简单的版本可以像这样显示:

 $('#parent').click(function() {
  $('.parent').modal('show');
});
    
$('#child').click(function() {
  $('.parent').modal('hide'); // Added to try and hide
  $('.child').modal('show');
  $('.parent').modal('hide'); // Added to try and hide
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parent">
  <div id="child">
  </div>
 </div>
  

问题是当我点击子 div 时,父对话框也会出现在子对话框后面。

有什么办法可以解决这个问题?

最佳答案

它正在发生,因为您的子点击事件正在冒泡到父级。对子 div 使用 e.stopPropogation()。这将阻止您的点击事件传播到父级。

$('#parent').click(function() {
  $('.parent').modal('show');
});
    
$('#child').click(function(e) {
  e.stopPropogation();
  $('.parent').modal('hide'); // Added to try and hide
  $('.child').modal('show');
  $('.parent').modal('hide'); // Added to try and hide
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parent">
  <div id="child">
  </div>
 </div>

关于javascript - jQuery/Bootstrap : Child DIV opens up Parent Modal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40106573/

相关文章:

javascript - Babel 没有从 JSX 转换为 JS,在不使用 webpack/browserify/bower 的情况下手动尝试

javascript - 仅更改悬停元素内的类

jquery 缓动图像

javascript - 根据包含字符串的值选择 <select> <option>

html - Bootstrap 列元素调整大小碰撞到新行

javascript - 如何在更改事件中获取复选框的数据元素

php - 将 en_US 转换为 en-US

php - 将 Backbone.js 模型插入 MySQL 数据库

javascript - 使用 jquery/js 将实际页面的 URL 传递到其他页面的输入字段中

javascript - Knockout Js应用绑定(bind)到多个html节点