我有一个弹出菜单,并在弹出窗口中有一个iframe代码作为文本。我有一个按钮来启用iframe代码文本。然后我需要禁用或显示:弹出窗口关闭时没有iframe文本。所以下次打开弹出窗口时,我必须再次点击按钮启用文本。我做了显示:没有iframe文本和弹出关闭按钮,但我无法通过单击弹出模式进行操作。当我将Display:none应用于模态类或淡化类时,我甚至无法启用文本。如何在弹出窗口关闭时禁用文本?
这是代码:
$(".embed-btn").on("click", function() {
$(".embed-iframe").css("display", "block");
});
$(".close").on("click", function() {
$(".embed-iframe").css("display", "none");
});
.embed-iframe{
display: none;
padding: 10px;
margin-top: 20px;
border: 1px solid #ececec;
background-color: #fafafa;
font-size: 14px;
font-weight: 400;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<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>
<button class="btn open" data-toggle="modal" data-target="#modal1">OPEN</button>
<div class="modal fade main-div-1" id="modal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-div" role="document">
<div class="col-md-3 img-div">
<img src="">
</div>
<div class="col-md-9 modal-content content-div">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<button class="btn embed-btn">Enable Code</button>
<div class="embed-iframe">
<span class="embed-iframe-text">
<iframe width="" height="" src="" frameborder="" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</span>
</div>
</div>
</div>
</div>
</div>
如何在弹出窗口关闭时禁用文本?
您可以使用Bootstrap的内置模式事件来处理此场景。
$('#modal1').on('hide.bs.modal', function (e) {
$(".embed-iframe").css("display", "none");
})
有了这个,你可以删除下面的代码行,因为.close
按钮也会触发hide.bs.modal
事件。
$(".close").on("click", function() {
$(".embed-iframe").css("display", "none");
});
你需要添加模态关闭事件监听器,你很高兴。
像这样
$('.modal').on('hidden.bs.modal', function() {
$(".embed-iframe").css("display", "none");
})
$(".embed-btn").on("click", function() {
$(".embed-iframe").css("display", "block");
});
$('.modal').on('hidden.bs.modal', function() {
$(".embed-iframe").css("display", "none");
})
.embed-iframe {
display: none;
padding: 10px;
margin-top: 20px;
border: 1px solid #ececec;
background-color: #fafafa;
font-size: 14px;
font-weight: 400;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<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>
<button class="btn open" data-toggle="modal" data-target="#modal1">OPEN</button>
<div class="modal fade main-div-1" id="modal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-div" role="document">
<div class="col-md-3 img-div">
<img src="">
</div>
<div class="col-md-9 modal-content content-div">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<button class="btn embed-btn">Enable Code</button>
<div class="embed-iframe">
<span class="embed-iframe-text">
<iframe width="" height="" src="" frameborder="" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</span>
</div>
</div>
</div>
</div>
</div>
你可以查看以下工作链接,http://jsfiddle.net/brahmpragya/mwcsbyv3/12/
在这,我解决了这个问题。只是一个小问题,在模态关闭隐藏iframe代码。
这是代码:
$(".embed-btn").on("click", function() {
$(".embed-iframe").css("display", "block");
});
$(".close").on("click", function() {
$(".embed-iframe").css("display", "none");
});
.embed-iframe{
display: none;
padding: 10px;
margin-top: 20px;
border: 1px solid #ececec;
background-color: #fafafa;
font-size: 14px;
font-weight: 400;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<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>
<button class="btn open" data-toggle="modal" data-target="#modal1">OPEN</button>
<div class="modal fade main-div-1" id="modal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-div" role="document">
<div class="col-md-3 img-div">
<img src="">
</div>
<div class="col-md-9 modal-content content-div">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<button class="btn embed-btn">Enable Code</button>
<div class="embed-iframe">
<span class="embed-iframe-text">
<iframe width="" height="" src="" frameborder="" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</span>
</div>
</div>
</div>
</div>
</div>