javascript - javascript - 两个按钮分别打开 2 个 iframe
问题描述
下午好,我在我的网站上使用了两个按钮,每个按钮打开一个不同的iframe
. iframe
问题是单击打开一个后,单击另一个按钮时另一个不会同时打开。
$(function() {
$('#load').click(function() {
if (!$('#iframe').length) {
$('#iframeHolder').html('<iframe id="iframe" scrolling="no" style="border:0px;" src="https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg" width="100%" height="700px;"></iframe>');
}
});
});
$(function() {
$('#load2').click(function() {
if (!$('#iframe').length) {
$('#iframeHolder2').html('<iframe id="iframe" scrolling="no" style="border:0px;" src="https://assets.wordpress.envato-static.com/uploads/2017/08/tropical-PS53BSA.jpg" width="100%" height="300px;"></iframe>');
}
});
});
<center>
<a style="background-color: rgb(98,124,169,0.8); color: white; cursor: pointer;" class="hashtagsocial" id="load">PODCASTS</a>
<a style="background-color: rgb(98,124,169,0.8); color: white; cursor: pointer;" class="hashtagsocial" id="load2">SCHEDULE</a>
</center>
<br>
<br>
<br>
<div id="iframeHolder"></div>
<br>
<br>
<div id="iframeHolder2"></div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
解决方案
因为这正是你if(!$('#iframe').length)
的两个按钮所阻止的。当您将新 HTML 附加到页面时,它会包含一个带有id="iframe"
. 因此,下次您运行该if
语句时,.length
大于0
所以检查false
并且该if
块不执行。
没有那个if
,你可以同时打开:
$(function(){
$('#load').click(function(){
$('#iframeHolder').html('<iframe scrolling="no" style="border:0px;" src="https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg" width="100%" height="700px;"></iframe>');
});
});
$(function(){
$('#load2').click(function(){
$('#iframeHolder2').html('<iframe scrolling="no" style="border:0px;" src="https://assets.wordpress.envato-static.com/uploads/2017/08/tropical-PS53BSA.jpg" width="100%" height="300px;"></iframe>');
});
});
<center><a style="background-color: rgb(98,124,169,0.8); color: white; cursor: pointer;" class="hashtagsocial" id="load">PODCASTS</a> <a style="background-color: rgb(98,124,169,0.8); color: white; cursor: pointer;" class="hashtagsocial" id="load2">SCHEDULE</a>
<br>
<br>
<br>
<div id="iframeHolder"></div>
<br>
<br>
<div id="iframeHolder2"></div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
注意:我还从附加的 HTML 中删除了id="iframe"
,因为您不想将两个相同的元素附加id
到页面。如果您需要这些元素具有 ID,它们必须是唯一的。
另一方面,如果您希望能够在两者之间切换,那么您可能希望将它们放在一个容器中,而不是放在它们自己的容器中:
$(function(){
$('#load').click(function(){
$('#iframeHolder').html('<iframe scrolling="no" style="border:0px;" src="https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg" width="100%" height="700px;"></iframe>');
});
});
$(function(){
$('#load2').click(function(){
$('#iframeHolder').html('<iframe scrolling="no" style="border:0px;" src="https://assets.wordpress.envato-static.com/uploads/2017/08/tropical-PS53BSA.jpg" width="100%" height="300px;"></iframe>');
});
});
<center><a style="background-color: rgb(98,124,169,0.8); color: white; cursor: pointer;" class="hashtagsocial" id="load">PODCASTS</a> <a style="background-color: rgb(98,124,169,0.8); color: white; cursor: pointer;" class="hashtagsocial" id="load2">SCHEDULE</a>
<br>
<br>
<br>
<div id="iframeHolder"></div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
另请注意,使用 2 个document.ready
处理程序是多余的。对于每个点击处理程序,您不需要那么多仪式,只需创建它们:
$(function(){
$('#load').click(function(){
$('#iframeHolder').html('<iframe scrolling="no" style="border:0px;" src="https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg" width="100%" height="700px;"></iframe>');
});
$('#load2').click(function(){
$('#iframeHolder').html('<iframe scrolling="no" style="border:0px;" src="https://assets.wordpress.envato-static.com/uploads/2017/08/tropical-PS53BSA.jpg" width="100%" height="300px;"></iframe>');
});
});
推荐阅读
- google-apps-script - 如何在 Google Apps 脚本中逐行删除重复项并逐列循环?
- c++ - 使用部分模板实例化删除限定符
- php - 如何将 Cloudflare 库添加到 codeigniter
- linux - 在汇编中使用参数调用 execve 的正确方法是什么?
- android - 我已经使用 recyclerview 实现了 searchview,但出现错误?
- c# - ViewDidAppear 导致我的标签消失(Xamarin.ios,C#)
- ruby - 如何测试我用 Ruby 编写的 ServerSpec 命令条件是否有效?
- google-cloud-firestore - 在 Firestore 中触发权限
- node.js - 在哪里存储用于查询的本体数据?
- c# - 更改安装项目 Visual Studio 的发布者名称