[英]Close a dialog by clicking the modal iframe?
I am creating a modal dialog box. 我正在创建一个模式对话框。 First I append an iframe to the body
tag to act as my overlay. 首先,我将一个iframe附加到body
标签上,作为我的叠加层。
<div class="modal-screen">
<iframe src="javascript:false;"></iframe>
</div>
Then I append my dialog box to the body
. 然后,将对话框附加到body
。
<div class="dialog">
<!-- various dialog related elements
</div>
Now I would like to close the dialog when the user clicks the overlay. 现在,我想在用户单击叠加层时关闭对话框。 The problem is that I can't seem to bind to any events related to the overlay. 问题是我似乎无法绑定到任何与叠加层相关的事件。
To summarize my JS, basically my dialog view has an initialize
method where the overlay is prepared: 总结一下我的JS,基本上我的对话框视图有一个initialize
方法,用于准备叠加层:
initialize: function() {
this.modal = $('<div class="modal-screen"><iframe src="javascript:false;"></iframe></div>')
this.modal.on('click', function(e) {
// this event never seems to fire
console.log("hello");
}
}
Then I render into the page when I need it. 然后在需要时渲染到页面中。 When I click the overlay, I never see hello
.. 当我单击叠加层时,我永远不会看到hello
..
render: function() {
$('body').append(this.modal);
// append other dialog content
this
}
this.modal is not a single element, instead it is a DOM object which is why you can not bind anything on it. this.modal不是单个元素,而是一个DOM对象,这就是为什么您不能在其上绑定任何内容的原因。 Try binding $('.modal-screen', this.modal) instead. 尝试改为绑定$('。modal-screen',this.modal)。
There is no reason to be using an iframe here. 这里没有理由使用iframe。 Simply construct an overlay div , then remove it when it is clicked. 只需构建一个overlay div ,然后单击它即可将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.