javascript - jQuery - 在页面上隐藏和显示特定的 iFrame

标签 javascript jquery html css iframe

我有一个网站,其中包含多个使用 iframe 的嵌入式 YouTube 视频。一开始我把它们全部隐藏起来,它们被缩略图遮住了。他们需要在单击缩略图时显示。

我有一个可行的解决方案,但它需要我向每个 iframe/缩略图添加类或 ID。我将无法向 iframe 或缩略图图像添加任何类或 ID。

这是我得到的:

$( document ).ready(function() {
<!-- Hide iFrames -->
$(".contentIframe1").hide();
$(".contentIframe2").hide();
    <!-- iFrame1 Click Function -->
    $(".play-thumbnail1").click(function(){
        $(".contentIframe1").show();
    });
    <!-- iFrame2 Click Function -->
    $(".play-thumbnail2").click(function(){
        $(".contentIframe2").show();
    });

});

HTML:

<iframe class="contentIframe1" width="1280" height="720" src="https://www.youtube.com/embedurl" frameborder="0" allowfullscreen></iframe>

  <div class="play-button"></div>
  <img class="play-thumbnail1" src="imgurl" alt="" />

如果不为每个缩略图和iframe添加不同的类,是否可以获得相同的效果?

这是页面上每个 iframe 的新 html:

<iframe width="1280" height="720" src="https://www.youtube.com/embedurl></iframe>

  <div class="play-button"></div>
  <img class="play-thumbnail" src="imgurl" alt="" />

这是新的 jQuery:

$( document ).ready(function() {
<!-- Hide iFrames -->
$("iframe").hide();
    <!-- iFrame Click Function -->
$(".play-thumbnail").click(function(){
$("iframe").show();
});     
});

这当然只是同时隐藏和显示它们。我认为我必须使用 .each() 来遍历每个缩略图并以某种方式使其仅显示相应的 iFrame。

这可能吗?

最佳答案

您可以使用 children() 获取对应的iframe

$(".play-thumbnail").click(function(){
    //if you need to hide currenly visible iframes use $('iframe').hide()
    $(this).closest('div').children("iframe").show();
});

关于javascript - jQuery - 在页面上隐藏和显示特定的 iFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29839178/

相关文章:

javascript - Bootstrap - 减少文本框和标签等表单控件之间的间距

html - 为什么 firefox 不能更好地渲染我的 GIF 文件?

javascript - Jquery Datatables 在 json 表中添加新行

jquery - 使用 Jquery 添加占位符属性

jquery - 简单的 jQuery 选择器只选择 Chrome 中的第一个元素..?

javascript - 如何处理 DOM 中的列表项

javascript - jQuery 固定 header 在滚动问题上向下滑动

javascript - 单例模式javascript

JavaScript:哪台机器进行计算?

javascript - 无法在 'createObjectURL' : 上执行 'URL'