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


HTML onended事件用法及代碼示例


音頻/視頻結束時,將發生HTML DOM onended事件。我們可以在此事件中添加一些自定義消息,例如“Thank for watching”,“Share”等。

用法:
在HTML中:

<element onended="myScript">

在JavaScript中:


object.onended = function(){myScript};

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

object.addEventListener("ended", myScript);

例:使用HTML

<!DOCTYPE html> 
<html> 
  
<body> 
    <center> 
        <h1 style="color:green">GeeksfroGeeks</h1> 
        <h2>HTML DOM onended Event</h2> 
        <audio controls onended="gfgFun()"> 
            <source src="beep.mp3" type="audio/mpeg"> 
        </audio> 
  
        <script> 
            function gfgFun() { 
                alert("Thanks for listning"); 
            } 
        </script> 
    </center> 
</body> 
  
</html>                

輸出:

例:使用JavaScript

<!DOCTYPE html> 
<html> 
  
<body> 
    <center> 
        <h1 style="color:green">GeeksfroGeeks</h1> 
        <h2>HTML DOM onended Event</h2> 
        <audio id="audioId" controls> 
            <source src="beep.mp3" type="audio/mpeg"> 
        </audio> 
  
        <p id="try"></p> 
  
        <script> 
            document.getElementById( 
                "audioId").onended = function() { 
                gfgFun() 
            }; 
  
            function gfgFun() { 
                document.getElementById( 
                    "try").innerHTML = "Thanks for listning"; 
            } 
        </script> 
    </center> 
</body> 
  
</html>

輸出:

例:使用addEventListener()方法

<!DOCTYPE html> 
<html> 
  
<body> 
    <center> 
        <h1 style="color:green">GeeksfroGeeks</h1> 
        <h2>HTML DOM onended Event</h2> 
        <audio id="audioId" controls> 
            <source src="beep.mp3" type="audio/mpeg"> 
        </audio> 
  
        <p id="demo"></p> 
  
        <script> 
            document.getElementById( 
              "audioId").addEventListener("ended", gfgFun); 
  
            function gfgFun() { 
                document.getElementById( 
                  "demo").innerHTML = "Thanks for listning"; 
            } 
        </script> 
    </center> 
</body> 
  
</html>

輸出:

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

  • 穀歌瀏覽器
  • Internet Explorer 9.0
  • Firefox
  • 蘋果Safari
  • Opera


相關用法


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