2017-06-23 191 views
0

我想播放/暫停,同時點擊視頻 我的代碼是在這裏,HTML5視頻播放器點擊數播放/暫停工作不

<!DOCTYPE html> 
<html> 
    <head> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
     <script type="javascript"> 
     $(document).ready(function() { 
      var myVideo = document.getElementById("video1"); 
      function playPause() 
      { 
      if (myVideo.paused) 
       myVideo.play(); 
      else 
       myVideo.pause(); 
      } 
      //your code here 
     }); 
     </script> 
    </head> 
    <body> 
     <video id="video1" onClick="playPause();" width="320" height="240" > 
      <source src="https://www.w3schools.com/html/movie.mp4" type="video/mp4"> 
     </video> 
    </body> 
</html> 

在哪裏我堅持?它顯示錯誤如ReferenceError: playPause is not defined

回答

1

你有幾個問題:

  • playPause是在頁面加載後才能確定,但​​你想之前綁定。您可以刪除$(document).ready()以在之前定義功能

  • document.getElementById("video")回報undefined(除非它是在你的後一個錯字)作爲<video>元素具有ID video1

這裏是一個工作代碼:

<!DOCTYPE html> 
<html> 
    <head> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 

     <script type="javascript"> 
     function playPause() { 
      var myVideo = $("#video1"); // or document.getElementById("video1"); 
      if (myVideo.paused) { 
       myVideo.play(); 
      } else { 
       myVideo.pause(); 
      } 

      // Your code here 
     } 
     </script> 
    </head> 
    <body> 
     <video id="video1" onClick="playPause();" width="320" height="240" autoplay="on"> 
      <source src="https://www.w3schools.com/html/movie.mp4" type="video/mp4"> 
     </video> 
    </body> 
</html> 
0

您的函數playPause()在這裏被定義爲一個匿名函數。

function() { 
      var myVideo = document.getElementById("video"); 
      function playPause() 
      { 
      if (myVideo.paused) 
       myVideo.play(); 
      else 
       myVideo.pause(); 
      } 
      //your code here 
     } 

這意味着playPause()的範圍只是這個匿名功能。這意味着你不能從任何你想要的地方打電話給它。

您應該在嘗試調用它之前對其進行定義。這樣的:

<!DOCTYPE html> 
<html> 
    <head> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
     <script type="javascript"> 
     function playPause(myVideo) 
      { 
      if (myVideo.paused) 
       myVideo.play(); 
      else 
       myVideo.pause(); 
      } 
      $(document).ready(function() { 
      var myVideo = document.getElementById("video"); 
      playPause(video); 
     }); 
     </script> 
    </head> 
    <body> 
     <video id="video1" onClick="playPause(document.getElementById("video"));" width="320" height="240" autoplay="on"> 
      <source src="https://www.w3schools.com/html/movie.mp4" type="video/mp4"> 
     </video> 
    </body> 
</html> 
0

有幾個問題代碼。主要是視頻的ID與您正在使用的視頻不同。試試這個片段。

  var myVideo = document.getElementById("video1"); 
 
      function playPause() 
 
      { 
 
      if (myVideo.paused) 
 
       myVideo.play(); 
 
      else 
 
       myVideo.pause(); 
 
      } 
 
      //your code here
<video id="video1" onClick="playPause();" width="320" height="240" autoplay="on"> 
 
      <source src="https://www.w3schools.com/html/movie.mp4" type="video/mp4"> 
 
     </video>