jQuery / Bootstrap: Child DIV打开父模式
jQuery / Bootstrap: Child DIV opens up Parent Modal
所以我已经嵌套了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时,父对话框也会出现在子对话框后面
有办法解决这个问题吗?
这是因为您的孩子点击事件正在冒泡到父。使用e.stopPropogation()
作为子div。这将防止您的点击事件传播到父div。
$('#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>
您可以通过在如下属性中提及目标来轻松处理这种情况。
$('#parent, #child').click(function() {
var target = $(this).data('target');
$('.'+target).modal('show');
// write code to hide others if necessary
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parent" data-target="parent">
<div id="child" data-target="child">
</div>
</div>
在显示模态之前试一下:
$( '.modal' ).remove();
$( '.child' ).modal('show');
在使用show之前删除模态。希望这将解决您的问题
相关文章:
- 添加文字和评论功能更新Div
- Javascript,访问一个主要对象模块模式中的每个对象
- 是否有任何snippet或jQuery插件可以列出easylist.txt模式匹配的DOM中的所有元素
- 试图在引导模式内动态生成图表,得到offsetWidth错误
- 同位素库错误:未捕获错误无布局模式包装生产线8
- 在DOM中查找一个模式并替换它's的内容使用jquery
- 点击后隐藏潜水?(但如果Div是一面旗帜呢?)
- 如何缩短MongoDB ObjectId并在Mongoose模式中使用它
- 使用JavaScript动态插入DIV的成本有多高
- 怪癖模式下的浮动DIV
- 在每个页面加载时显示随机 DIV(对于花哨的框模式)
- 将 UI 模式对话框添加到新的 DIV 标记
- Div 中的文本/字体大小在 Firefox 的打印模式下与在“正常”模式下不同
- 在引导模式中滚动到DIV
- IE7标准文档模式中SPAN/DIV高度不正确
- jQuery / Bootstrap: Child DIV打开父模式
- 在RTL模式下获得可滚动DIV的视图的更好方法
- Bootstrap模式禁用子DIV中的文本字段
- 如何将缩略图图像附加到 DIV 时以特定模式定位它们
- 引导模式替换按钮链接-动态Div id