简体   繁体   English

jQuery删除vimeo iframe

[英]jquery remove vimeo iframe

I have a small script that loads a vimeo video into a div onclick and then a button that "should" remove it onclick. 我有一个小脚本,可将vimeo视频加载到div onclick中,然后单击“应”将其onclick删除。 needless to say I wouldn't be posting here if it was working and if i hadn't already found a working solution elsewhere in stack so...here my code: 不用说,如果它正在运行,并且如果我还没有在堆栈中的其他地方找到有效的解决方案,那么我不会在这里发布它,所以...这里是我的代码:

$('.play_button').click(function() {
  $( ".play_border, .styled-header" ).remove();
  $( ".video-container" ).append( '<iframe id="vimeo" src="//player.vimeo.com/video/103277178?autoplay=1" width="100%" height="306" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe><div class="form-transparent"><button class="btn pull-right close_video" type="button">Close Video</button>');
});
$('.close_video').click(function() {
  $( "#vimeo" ).remove();
  $( ".video-container" ).append( '<div class="play_border"><div class="play_button"></div></div><h2 class="white styled-header">Play Our Video</h2>');
});

and the html: 和html:

<div class="video-container">
    <div class="play_border">
        <div class="play_button"></div>
    </div>
    <h2 class="white styled-header">Play Our Video</h2>
</div>

Your listener isn't firing because you are listening for the close_video before it has been appended to the page. 您的监听器无法触发,因为您正在监听close_video,但尚未将其添加到页面。 Since you are adding it to the page you will need to use event delegation. 由于要将其添加到页面,因此需要使用事件委托。

$('.video-container').on('click', '.close_video', function() {
  $( "#vimeo" ).remove();
  $( ".video-container" ).append( '<div class="play_border"><div class="play_button"></div> </div><h2 class="white styled-header">Play Our Video</h2>');
});

As mentioned by beaglebets your listener isn't firing, because you are trying to add it to close_video before it exists. 如beaglebets所述,您的监听器不会触发,因为您正在尝试将其添加到close_video之前就存在。 You could do it this way instead: 您可以这样操作:

function show_video() {
  $( ".play_border, .styled-header" ).remove();
  $( ".video-container" ).append( '<iframe id="vimeo" src="//player.vimeo.com/video/103277178?autoplay=1" width="100%" height="306" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe><div class="form-transparent"><button class="btn pull-right close_video" type="button">Close Video</button>');
  $( ".close_video" ).click(hide_video);
}

function hide_video() {
  $( "#vimeo, .close_video" ).remove();
  $( ".video-container" ).append( '<h2 class="white styled-header">Play Our Video</h2><div class="play_border"><button class="btn play_button" type="button">Play</button></div>');
  $( ".play_button" ).click(show_video);
}

$( ".play_button" ).click(show_video);

http://jsfiddle.net/L5jcLLxz/ http://jsfiddle.net/L5jcLLxz/

(I changed your HTML code slightly, so that there is actually something to click for play_button.) (我略微更改了您的HTML代码,因此实际上可以单击play_button。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM