播放iframe视频点击链接javascript [英] play iframe video on click a link javascript

查看:808
本文介绍了播放iframe视频点击链接javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网页上使用了 iframe视频。这是我的HTML代码

I have used a iframe video in my web page. This is my html code

<iframe id="video1" width="520" height="360" src="http://www.youtube.com/embed/TJ2X4dFhAC0?enablejsapi" frameborder="0" allowtransparency="true" allowfullscreen></iframe>
<a href="#" id="playvideo">Play video</a>

我需要播放视频 onclick 播放视频链接。我怎么能这样做?

I need to play video onclick the play video link. How can i do that?

推荐答案

这样可行,它附加 autoplay = 1 导致视频开始播放的网址。

This works, it appends autoplay=1 to the url causing the video to start playing.

附录:如果您的视频来源还没有查询字符串,那么添加是否明智? / code>而不是& ,有时就是这种情况。这可以通过查找它的存在来完成。

addendum: If your video's source does not already have a querystring then it would be prudent to add a ? instead of a &, as is sometimes the case. This can be done by looking for its existence.

<iframe id="video1" width="520" height="360" src="http://www.youtube.com/embed/TJ2X4dFhAC0?enablejsapi" frameborder="0" allowtransparency="true" allowfullscreen></iframe>
<a href="#" id="playvideo">Play video</a>
<script>
 //use .one to ensure this only happens once
 $("#playvideo").one(function(){
  //as noted in addendum, check for querystring exitence
  var symbol = $("#video1")[0].src.indexOf("?") > -1 ? "&" : "?";
  //modify source to autoplay and start video
  $("#video1")[0].src += symbol + "autoplay=1";
 });
</script>

然而,大多数人天生就明白,如果他们想要播放视频,他们只需点击它我建议你把它留给他们或用自动播放开始播放视频。

However, most people inherently understand that if they want a video to play, they will just click on it and I would suggest just leaving that to them or starting the video off with autoplay.

还需要提一下,自动播放在移动设备上不起作用(由Android或iOS驱动) )

Also need to mention that autoplay does not work on mobile devices (powered by Android or iOS)

这篇关于播放iframe视频点击链接javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆