如何在divbox Javascript中添加YouTube嵌入(iframe)

问题描述 投票:-1回答:1

如何使用文本输入框,提交按钮和javascript在Divbox中添加YouTube嵌入式iframe?

谢谢。

javascript iframe youtube embedding
1个回答
0
投票

通过将“ Divbox”表示为“ div”,我尝试了一个答案:如果用户知道视频ID(YouTube网址末尾的字符串),则可以执行以下操作:

document.getElementById("b").onclick = function(d) {
  var full = '<iframe width="560" height="315" src="https://www.youtube.com/embed/' + document.getElementById("inp").value + '" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'
  document.getElementById("frame").innerHTML = full;
}
<button id="b">Go!</button>
<input id="inp" value="MPZI2M1fDi8"/>
<div id="frame"></div>
© www.soinside.com 2019 - 2024. All rights reserved.