Javascript play() youtube vid 在 Firefox 中不起作用
Javascript play() youtube vid not working in firefox
我正在播放带有play((函数的嵌入式YouTube视频。这一切都是使用youtube api。下面是我的代码。它在chrome和IE中完美运行,但在Firefox中无法播放。
alert(typeof ytplayer);
这段代码显示了ytplayer是什么类型的"东西"。在 chrome 和 IE 中,它将提供对象(视频嵌入了对象方法(。在火狐中,它说未定义,所以那里出了点问题。
我这里有一个测试网站:http://lostinawesome.blogspot.com.au/2012/04/test-post.html
点击 玩 按钮或图像以播放YouTube音频。在火狐中,它可能不适合你。
有什么想法可以让它在FF中发挥作用吗?
我的代码:
JavaScript:
function onYouTubePlayerReady(playerId) {
var player = document.getElementById("ytplayer");
player.addEventListener("onStateChange", "onytplayerStateChange");
}
function onytplayerStateChange(newState) {
alert("New state " + newState);
}
function play() {
alert(typeof ytplayer);
if (ytplayer) {
ytplayer.playVideo();
}
}
.html:
<object data="http://www.youtube.com/v/u8C-ZTQJIkU?enablejsapi=1&playerapiid=ytplayer&version=3" height="400" id="ytplayer" type="application/x-shockwave-flash" width="550"><param name="allowScriptAccess" value="always">
<param name="movie" value="http://www.youtube.com/v/u8C-ZTQJIkU?enablejsapi=1&playerapiid=ytplayer&version=3" >
</param>
<param name="allowScriptAccess" value="always">
</param>
</object>
<a href="javascript:void(0);" onclick="play();">Play</a>
工作代码:
我只需要更改为 var ytplayer = document.getElementById("ytplayer"(;因为 ytplayer 没有在任何地方声明为变量,但由于某种原因,它无论如何都可以在 Chrome 和 IE 中工作。
function play() {
var ytplayer = document.getElementById("ytplayer");
alert(typeof ytplayer);
if (ytplayer) {
ytplayer.playVideo();
}
}
您需要显式选择元素,而不仅仅是使用其 ID,因为它不会在所有浏览器中都有效,如您所见。
更改为
var ytplayer = document.getElementById('ytplayer');
ytplayer.playVideo();
相关文章:
- 为什么javascript:void(0)在Firefox中不起作用
- jpm的默认Firefox路径没有'不起作用
- 设置 CSS 背景在 Firefox 中不起作用
- document.getElementById 在 Firefox 中不起作用
- firefox中的CSS动画不起作用
- event.stopPropagation()在firefox javascript中不起作用
- 复选框.onClick在firefox中不起作用
- window.close();在Chrome和Firefox中不起作用
- JQuery在Chrome中不起作用,但Firefox很好
- 选择“行在 Chrome 和 Firefox 中不起作用(生成的代码)”
- Javascript 按键事件在 Firefox 中不起作用
- window.dispatchEvent 在 Firefox、Safari 或 IE 中不起作用
- 更改 Angular 2 中选择的事件在 Firefox 和 Edge 中不起作用
- jQuery SlideToggle() 在 FireFox 中不起作用,在 Chrome 中工作
- 动态关键帧插入在 IE 或 Firefox 上不起作用
- 使用 nameProp 的 JQuery 图像滚动更新在 Firefox 和 Chrome 中不起作用
- Javascript键码在Firefox中不起作用,但在chrome中运行良好
- HTML5<视频>元素在Firefox、Opera和Safari中不起作用
- 输入类型的图像在Internet Explorer中不起作用,但在Chrome和Firefox中起作用
- 使用量角器双击在chrome中效果良好,但在firefox中不起作用