javascript - 通过外部按钮在 Iframe 中播放视频
问题描述
是否可以从外部按钮开始播放 YouTube iframe 的视频?
<button>Play Button</button>
<iframe width="560" height="315" src="https://www.youtube.com/embed/HJtJXMKpl2g" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
免责声明:iframe 视频不属于我。
解决方案
由于此视频位于 iframe 和另一个主机中,出于安全原因,您无法通过以下方式访问该 iframe 的内部结构
const iframe = document.getElementById("myFrame");
const video = iframe.contentWindow.document.getElementsByTagName("video")[0];
video.play();
添加"autoplay=1"
到 src 可能会有所帮助,但它只能启动一次 video
iframe.setAttribute('src', iframe.getAttribute('src') + "?autoplay=1")
推荐阅读
- java - Guava 的 ListenableFuture 响应速度如何
- scala - 路径不存在时在输出中打印列值
- xslt - 考虑标签和值的 XSL 删除重复记录
- scala - Scala:按小时范围过滤字符串日期?
- android - Kotlin 如何在 MPAndroidChart 中使用实时数据库
- matlab - 特定值的符号函数的导数
- file-upload - 使用 react-hook-form 上传的图像字段在前端工作,但在 next.js API 路由中,req.body.image 是一个空对象
- django-rest-framework - 进行 django rest 调用后的渲染模板
- build - 管道构建后如何从 gitlab 发送邮件
- oracle - Oracle APEX 中的文档