更改iframe src会阻止父级隐藏

时间:2016-04-07 17:23:26

标签: javascript jquery html css iframe

我有以下HTML。

<div id="video-popup-overlay"></div>

<div id="video-popup-container">
    <div id="video-popup-close" class="fade"><i class="fa fa-times"></i></div>
    <div id="video-popup-iframe-container">
        <iframe id="video-popup-iframe" src="" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
    </div>
</div>

基本上我在显示#video-popup-container时播放视频。我希望能够隐藏此视频。但通常在隐藏元素时,视频仍会在后台播放。

这就是我想要更改src的原因。但是当我更改src时,元素不会隐藏。但是如果我不更改src,则hide命令会起作用。

这是否会发生这种情况?

$("#video-popup-close, #video-popup-overlay").on('click', function(e){
    $("#video-popup-iframe").attr('src', "");
    $("#video-popup-overlay").hide();
    $("#video-popup-container").hide();
});

Here is an example

3 个答案:

答案 0 :(得分:1)

是否需要在隐藏后再次显示?由于您将iframe中的src参数设置为空白,我可能会假设不是吗?

如果是这种情况,我建议尝试使用.remove()功能而不是隐藏。即:

$("#video-popup-close, #video-popup-overlay").on('click', function(e){
     $("#video-popup-overlay, #video-popup-container").remove(); 
});

更新:

根据提供的小提示,看起来你正在调用错误的ID来关闭。在此查看小提琴:https://jsfiddle.net/g139c9b0/2/

$("#video-popup-close, #video-popup-overlay").on('click', function(e){
    $("#video-popup-overlay, #video-popup-iframe-container, #video-popup-close").hide();
    $("#video-popup-iframe").attr('src', '');
});

更新更新: 对不起,我认为这足以推断出来。是的,您必须添加show()函数,因为您隐藏了元素。这是完全的小提琴,正如我认为你期望的那样:https://jsfiddle.net/g139c9b0/3/

我最好猜测为什么你看到奇怪的行为是iframe本身不被视为当前DOM的一部分,所以隐藏它的容器不一定会级联到iframe窗口。通常,您无法使用javascript与iframe交互(没有一些解决方法)。它肯定感觉像是一个浏览器错误,但出于安全原因,它可能会起作用。

答案 1 :(得分:1)

你正在播放什么样的视频? 是本地托管文件,YouTube视频,Vimeo?我问,因为如果是YouTube,也许你可以使用jQuery来停止视频。

这就是说,我已经测试了你的代码,它似乎工作正常。你是否在浏览器控制台上出现任何错误?

   

 $(".clickme").on('click', function(e){ 
		e.preventDefault();
		$("#video-popup-iframe").attr('src', "//player.vimeo.com/video/"+$(this).attr('id'));
		$("#video-popup-overlay, #video-popup-container").show(); 
    });

$("#video-popup-close, #video-popup-overlay").on('click', function(e){
	
		$("#video-popup-overlay, #video-popup-container").hide();
    	$("#video-popup-iframe").attr('src', '');
	});
#video-popup-overlay {
  display: none;
  position: fixed;
  z-index: 995;
  top: 0;
  background-color: #000;
  opacity: 0.8;
  width: 100%;
  height: 100%;
}

#video-popup-container {
  display: none;
  position: fixed;
  z-index: 996;
  width: 60%;
  left: 50%;
  margin-left: -30%;
  top: 20%;
  background-color: #fff;
}

#video-popup-close {
  cursor: pointer;
  position: absolute;
  right: -10px;
  top: -10px;
  z-index: 998;
  width: 25px;
  height: 25px;
  border-radius: 25px;
  text-align: center;
  font-size: 12px;
  background-color: #000;
  line-height: 25px;
  color: #fff;
}

#video-popup-close:hover {
  color: #DE0023;
}

#video-popup-iframe-container {
  position: absolute;
  z-index: 997;
  width: 100%;
  padding-bottom: 56.25%;
  border: 2px solid #000;
  background-color: #000;
  border-radius: 2px;
}

#video-popup-iframe {
  z-index: 999;
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="clickme" id="161171581">
Click me
</div>

<div id="video-popup-container">
  <div id="video-popup-close" class="fade">x</div>
  <div id="video-popup-iframe-container">
    <iframe id="video-popup-iframe" src="" width="100%" height="100%" frameborder="0"></iframe>
  </div>
</div>

另一种可能的解决方案: Stop embedded youtube iframe?

编辑:看一下更新的代码,这是预期的行为吗? EDIT2:https://jsfiddle.net/588chuyb/

答案 2 :(得分:0)

在更改src之前尝试隐藏。并查看控制台,看看是否有消息,它应该有帮助。

$(document).on('click', '#video-popup-close, #video-popup-overlay', function(e){
    $("#video-popup-overlay, #video-popup-container").hide();
    $("#video-popup-iframe").attr("src", "");
});