隐藏的YouTube播放器失去了它的方法 [英] Hidden youtube player loses its methods

查看:256
本文介绍了隐藏的YouTube播放器失去了它的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用javascript控制一个嵌入式的youtube chromeless播放器,我想偶尔通过设置display:none来隐藏它。但是,当我再次显示玩家时,会丢失它的youtube方法。例如:



$ p

> <脚本>
swfobject.embedSWF(http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=player,
player,425,356,8, null,null,
{allowScriptAccess:always},{id:'player'}
);

var player = null;

function onYouTubePlayerReady(playerId){
player = document.getElementById(playerId);
player.addEventListener('onStateChange','playerStateChanged');
}

function hidePlayer(){
player.pauseVideo();
player.style.display ='none';
}
function showPlayer(){
player.style.display ='block';
player.playVideo();
}
< / script>
< a href =#onClick =hidePlayer();>隐藏< / a>
< a href =#onClick =showPlayer();>显示< / a>
< div id =player>< / div>

调用hidePlayer后跟showPlayer会在playVideo调用中产生这个错误:

  Uncaught TypeError:Object#< an HTMLObjectElement>没有方法'playVideo'

唯一的解决办法是使用visibility:hidden,但是正在搞乱我的页面布局。任何其他的解决方案吗?

解决方案

我在我的脚本中遇到过这种情况。你可以尝试使它成为1x1像素,而不是完全隐藏它。



这是一个相当烦人的问题,为什么会发生这种情况没有意义。


I'm controlling a embedded youtube chromeless player with javascript, and I want to hide it occasionally by setting display: none. However, when I show the player again, it loses its youtube methods.

For example:

<script>
  swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=player",
    "player", "425", "356", "8", null, null, 
    {allowScriptAccess: "always"}, {id: 'player'}
  );

  var player = null;

  function onYouTubePlayerReady(playerId) {
    player = document.getElementById(playerId);
    player.addEventListener('onStateChange', 'playerStateChanged');
  }

  function hidePlayer() {
    player.pauseVideo();
    player.style.display = 'none';
  }
  function showPlayer() {
    player.style.display = 'block';
    player.playVideo();
  }
</script>
<a href="#" onClick="hidePlayer();">hide</a>
<a href="#" onClick="showPlayer();">show</a>
<div id="player"></div>

Calling hidePlayer followed by showPlayer gives this error on the playVideo call:

Uncaught TypeError: Object #<an HTMLObjectElement> has no method 'playVideo'

The only solution I can find is to use visibility: hidden, but that is messing with my page layout. Any other solutions out there?

解决方案

I've had this happen to me as well in my userscript. You can try just making it 1x1 px, instead of hiding it altogether.

It's a pretty annoying issue, and makes no sense as to why this happens.

这篇关于隐藏的YouTube播放器失去了它的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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