當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


HTML oncanplay事件用法及代碼示例


當指定的音頻/視頻緩衝到足以開始時,就會發生HTML DOM oncanplay事件。

事件的發生順序在音頻/視頻的加載過程中:

  1. onloadstart
  2. ondurationchange
  3. onloadedmetadata
  4. onloadeddata
  5. onprogress
  6. oncanplay
  7. oncanplaythrough

用法:


在HTML中:

<element oncanplay="myScript">

在JavaScript中:

object.oncanplay = function(){myScript};

在JavaScript中,使用addEventListener()方法:

object.addEventListener("canplay", myScript);

例:使用HTML

<!DOCTYPE html> 
<html> 
  
<body> 
    <center> 
        <h1 style="color:green"> 
          GeeksforGeeks 
      </h1> 
        <h2>HTML DOM oncanplay event</h2> 
  
        <video controls oncanplay="myFunction()"> 
            <source src="Geekfun.mp4" type="video/mp4"> 
        </video> 
  
        <script> 
            function myFunction() { 
                alert("Can start playing video"); 
            } 
        </script> 
    </center> 
</body> 
  
</html>

輸出:

例:使用JavaScript

<!DOCTYPE html> 
<html> 
  
<body> 
    <center> 
        <h1 style="color:green"> 
          GeeksforGeeks 
      </h1> 
        <h2>HTML DOM oncanplay event</h2> 
  
        <video controls id="myVideo"> 
            <source src="Geekfun.mp4" type="video/mp4"> 
        </video> 
  
        <script> 
            document.getElementById("myVideo").oncanplay = function() { 
                myFunction() 
            }; 
  
            function myFunction() { 
                alert("Can start playing video"); 
            } 
        </script> 
    </center> 
</body> 
  
</html>

輸出:

例:在JavaScript中,使用addEventListener()方法:

<!DOCTYPE html> 
<html> 
  
<body> 
    <center> 
        <h1 style="color:green"> 
          GeeksforGeeks 
      </h1> 
        <h2>HTML DOM oncanplay event</h2> 
  
        <video controls id="myVideo"> 
            <source src="Geekfun.mp4" type="video/mp4"> 
        </video> 
  
        <script> 
            document.getElementById( 
              "myVideo").addEventListener( 
              "canplay", myFunction); 
  
            function myFunction() { 
                alert("Can start playing video"); 
            } 
        </script> 
    </center> 
</body> 
  
</html>

輸出:

支持的瀏覽器:DOM oncanplay Event支持的瀏覽器如下:

  • 穀歌瀏覽器
  • IE瀏覽器
  • Firefox
  • 蘋果Safari
  • Opera


相關用法


注:本文由純淨天空篩選整理自Vijay Sirra大神的英文原創作品 HTML | DOM oncanplay Event。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。