Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么会赢';你不会被解雇吗?_Javascript_Jquery - Fatal编程技术网

Javascript 为什么会赢';你不会被解雇吗?

Javascript 为什么会赢';你不会被解雇吗?,javascript,jquery,Javascript,Jquery,我试图制作一个按钮开始在播放器上播放视频。 如果我按控制栏上的“播放”,播放机将显示并正常工作。 但是我的a href链接无法开始播放。 play()不会被激发。为什么? <script> function onYouTubePlayerReady(playerId) { ytplayer = document.getElementById("myytplayer"); } function play() { if (ytplayer) {

我试图制作一个按钮开始在播放器上播放视频。
如果我按控制栏上的“播放”,播放机将显示并正常工作。
但是我的
a href链接
无法开始播放。
play()
不会被激发。为什么?

<script>
    function onYouTubePlayerReady(playerId) {
      ytplayer = document.getElementById("myytplayer");
    }

    function play() {
  if (ytplayer) {
    ytplayer.playVideo();
  }
}
</script>

  <script type="text/javascript" src="swfobject.js"></script>    
  <div id="ytapiplayer">
    You need Flash player 8+ and JavaScript enabled to view this video.
  </div>

  <script type="text/javascript">

    var params = { allowScriptAccess: "always" };
    var atts = { id: "myytplayer" };
    swfobject.embedSWF("http://www.youtube.com/v/P5_GlAOCHyE?enablejsapi=1&playerapiid=ytplayer", 
                       "ytapiplayer", "425", "356", "8", null, null, params, atts);

  </script>

  <a href="javascript:void(0);" onclick="play();">Play</a>

函数onYouTubePlayerReady(playerId){
ytplayer=document.getElementById(“myytplayer”);
}
函数播放(){
如果(玩家){
ytplayer.playVideo();
}
}
您需要启用Flash player 8+和JavaScript才能查看此视频。
var params={allowScriptAccess:“始终”};
var atts={id:“myytplayer”};
swfobject.embeddeswf(“http://www.youtube.com/v/P5_GlAOCHyE?enablejsapi=1&playerapiid=ytplayer", 
“ytapiplayer”、“425”、“356”、“8”、空、空、参数、atts);

与Flash对象交谈可能很困难。以下是我用来发送消息的代码:

function runFlashFunction( flashName, funcName ) {
    var
    od = document[flashName],
    ow = window[flashName];
    // flash isn't happy with o=func;o(); syntax
    try {
        if( od && !!(od[funcName]) ) {
            return document[flashName][funcName]( );
        }
        if( ow && !!(ow[funcName]) ) {
            return window[flashName][funcName]( );
        }
        if( ow && ow.length && !!(ow[0]) && !!(ow[0][funcName]) ) {
            return window[flashName][0][funcName]( );
        }
        return void 0;
    } catch( ex ) {
        return void 0;
    }
}
为什么会这样?因为有些浏览器使用不同的方式访问flash内容。最后一个可能是不需要的,但我包括它以确保

此外,您的链接使用了不推荐的样式。这样更好:

<a href="#" onclick="play();return false">Play</a>

尽量不要使用全局变量

var ytplayer = null;
function onYouTubePlayerReady(playerId) {
   ytplayer = document.getElementById("myytplayer");
}

function play() {
  if (ytplayer) {
    ytplayer.playVideo();
  }
}

检查浏览器控制台是否有任何错误errors@ArunPJohny啊,这太荒谬了。它在FireFox上有效,但在Safari上有效。为什么不支持Mac???@SalmanA为什么不需要?那么如何调用函数onYouTubePlayerReady?注意:我没有使用
swfobject
库before@Arun:YouTube API专门检查此函数,如果它存在,则激发它。@SalmanA您能给我看一个代码示例吗?谢谢。但是你的解释并没有提到代码还需要什么。
var ytplayer = null;
function onYouTubePlayerReady(playerId) {
   ytplayer = document.getElementById("myytplayer");
}

function play() {
  if (ytplayer) {
    ytplayer.playVideo();
  }
}