微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 即使用document.getElementById(‘xyz’)也无法控制Youtube嵌入.playVideo() – 不是函数?

好吧,我被卡住了,即使在关注了Google的文档并在Stackoverflow上阅读建议之后我也不知道出了什么问题.为什么我无法在网页中控制Youtube嵌入?

如果我使用< body>创建HTML文件存在:

<object id="o1" width="480" height="295">
  <param name="movie" 
    value="http://www.youtube.com/v/qCTLCNmnlKU&hl=en_US&fs=1&enablejsapi=1&">
  </param>
  <param name="allowFullScreen" value="true"></param>
  <param name="allowscriptaccess" value="always"></param>
  <embed id="e1" 
    src="http://www.youtube.com/v/qCTLCNmnlKU&hl=en_US&fs=1&enablejsapi=1&" 
    type="application/x-shockwave-flash" 
    allowscriptaccess="always" allowfullscreen="true" width="480" height="295">
  </embed>
</object>

即使我试图这样做:

// I get an object. Yay.

document.getElementById('e1');

// This generates "...playVideo is not a function"

document.getElementById('e1').playVideo();

救命!我究竟做错了什么?谢谢.

解决方法:

好的,所以这是API页面上一小段文字的答案:http://code.google.com/apis/youtube/js_api_reference.html

“Note: To test any of these calls, you must have your file running on a webserver, as the Flash player restricts calls between local files and the internet.”

因此,为了让我继续在Mac笔记本电脑上进行开发,我做了以下工作:

>编辑我的/ etc / hosts文件以包含一个返回我的localhost的条目:

127.0.0.1   testhost.com

>编辑我的/etc/apache2/httpd.conf文件添加指向我的开发目录的虚拟主机条目:

<VirtualHost *:80>
    ServerName testhost.com
    DocumentRoot /Users/amy/flashproj
    <Directory /Users/amy/flashproj>
        AllowOverride all
        Options MultiViews Indexes FollowSymLinks
        Allow from All
    </Directory>
</VirtualHost>

>重启Apache:

sudo apachectl restart

>通过我的新虚拟服务器浏览回我自己的localhost:

http://testhost.com

瞧.这完全有效.我可以查询播放器的页面

document.getElementById('e1');                // OK
document.getElementById('e1').playVideo();    // OK!

呼!没有onYouTubePlayerReady()也需要!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐