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


HTML ondurationchange事件用法及代碼示例


更改音頻/視頻持續時間時,將發生HTML DOM ondurationchange事件。音頻/視頻的持續時間從“NaN”更改為加載時的音頻/視頻的實際持續時間。

音頻/視頻的加載過程中發生以下事件:

  • onloadstart
  • ondurationchange
  • onloadedmetadata
  • onloadeddata
  • onprogress
  • oncanplay
  • oncanplaythrough

用法:


  • 在HTML中:
    <element ondurationchange="myScript">
  • 在JavaScript中:
    object.ondurationchange = function(){myScript};
  • 在JavaScript中,使用addEventListener()方法:
    object.addEventListener("durationchange", myScript);

範例1:使用HTML

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM ondurationchange Event 
    </title> 
</head> 
  
<body> 
    <center> 
        <h1 style="color:green">GeeksforGeeks</h1> 
        <h2>HTML DOM ondurationchange Event</h2> 
        <video controls ondurationchange="GFGfun()"> 
            <source src="Canvas.move_.mp4"
                    type="video/mp4"> 
        </video> 
  
        <script> 
            function GFGfun() { 
                alert("The video duration has changed"); 
            } 
        </script> 
    </center> 
</body> 
  
</html>

輸出:

範例2:使用JavaScript

<!DOCTYPE html> 
<html> 
  
<head> 
    <title>HTML DOM ondurationchange Event</title> 
</head> 
  
<body> 
    <center> 
        <h1 style="color:green">GeeksforGeeks</h1> 
        <h2>HTML DOM ondurationchange Event</h2> 
        <video controls ondurationchange="GFGfun()"> 
            <source src="Canvas.move_.mp4" type="video/mp4"> 
        </video> 
  
        <script> 
            document.getElementById("durVideo").ondurationchange = function() { 
                GFGfun() 
            }; 
  
            function GFGfun() { 
                alert("The video duration has changed"); 
            } 
        </script> 
    </center> 
</body> 
  
</html>

輸出:

範例3:使用addEventListener()方法:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
      HTML DOM ondurationchange Event 
  </title> 
</head> 
  
<body> 
    <center> 
        <h1 style="color:green">GeeksforGeeks</h1> 
        <h2>HTML DOM ondurationchange Event</h2> 
        
        <video controls ondurationchange="GFGfun()"> 
            <source src="mov_bbb.mp4" type="video/mp4"> 
        </video> 
  
        <script> 
            document.getElementById( 
              "durVideo").addEventListener("durationchange", GFGfun); 
  
            function GFGfun() { 
                alert("The video duration has changed"); 
            } 
        </script> 
    </center> 
</body> 
  
</html>

輸出:

支持的瀏覽器:下麵列出了HTML DOM ondurationchange Event支持的瀏覽器:

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


相關用法


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