【问题标题】:YouTube: play YouTube video just when click buttonYouTube:只需单击按钮即可播放 YouTube 视频
【发布时间】:2021-01-07 16:14:27
【问题描述】:

我的页面包含嵌入在 Iframe 中的 YouTube,我让它无法点击,我想在点击播放按钮时播放视频,我在这里和网络上搜索并测试了它们,但它们不起作用:

<div class="col-md-6">
        <div class='py-3' ><?php echo $course_name?></div>
        <div class="embed-responsive embed-responsive-16by9 px-6 py-6" style='pointer-events: none'>
            <iframe  id='video'
                src="<?php echo $course_video?>">  <!--the Youtube livk is come  from server as : https://www.youtube.com/embed/xxxxxxxxxxxx -->
            </iframe>
        </div>
        <div class='row w-100 justify-content-center mt-3'><button  class='btn bg-primary' id='play_video'><i class=" fa fa-play"></i></button></div>
</div>

还有我的 JavaScript 代码:

$(document).ready(function(){
        $('#play_video').on('click', function(ev) {
            $("#video")[0].src += "&autoplay=1";
            ev.preventDefault();
        })

})

我该怎么做?

【问题讨论】:

    标签: javascript php video iframe youtube


    【解决方案1】:

    您忘记在 JavaScript 中添加分号:

    $(document).ready(function() {
      $('#play-video').on('click', function(ev) {
        $("#video")[0].src += "&autoplay=1";
        ev.preventDefault();
     
      });
    });
    

    您还忘记将链接添加到您的 HTML 文件中,单击 (&lt;a id="play-video" href="#"&gt;Play Video&lt;/a&gt;&lt;br /&gt;) 并从您的代码中删除 style='pointer-events: none'

    <div class="col-md-6">
        <div class='py-3' ><?php echo $course_name?></div>
            <div class="embed-responsive embed-responsive-16by9 px-6 py-6">
                <a id="play-video" href="#">Play Video</a><br />
                <iframe  id='video' src="<?php echo $course_video?>">
                </iframe>
            </div>
    </div>
    

    【讨论】:

    • 谢谢,但我有可以点击的按钮:
    • 是的,不仅仅是按钮。看看剩下的。例如,您必须删除 style='pointer-events: none'
    • 它只是通过删除按钮标签并将点击功能设置为图标来解决,并且仍然可以通过不删除指针事件来工作......谢谢
    猜你喜欢
    • 2014-06-16
    • 2021-11-03
    • 2012-07-23
    • 1970-01-01
    • 2017-02-22
    • 2017-07-31
    • 2014-08-30
    • 2013-06-12
    • 2017-05-11
    相关资源
    最近更新 更多