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


HTML onplay屬性用法及代碼示例

HTML onplay屬性是播放音頻/視頻時出現的evenr屬性。音頻/視頻可以由用戶播放,也可以通過程序播放。

用法:

<element onplay="myScript">

屬性值:此屬性包含單值腳本,該腳本在onplay事件屬性調用時起作用。 <audio>和<video>標簽支持此屬性。

範例1:

<!DOCTYPE html> 
<html> 
    <head> 
        <title> 
            HTML onplay Attribute 
        </title> 
    </head> 
  
    <body> 
        <center> 
            <h1 style="color:green;"> 
                GeeksforGeks 
            </h1> 
            <h2>HTML onplay Attribute</h2> 
  
            <audio controls id="audID" 
                   onplay="GFGfun()"> 
                <source src= 
"https://media.geeksforgeeks.org/wp-content/uploads/20190625153922/frog.mp3"
                        type="audio/mpeg" /> 
            </audio> 
  
            <script> 
                function GFGfun() { 
                    alert("audio play"); 
                } 
            </script> 
        </center> 
    </body> 
</html>

輸出:



單擊播放按鈕後:

範例2:

<!DOCTYPE html> 
<html> 
    <head> 
        <title> 
            HTML onplay Attribute 
        </title> 
    </head> 
  
    <body> 
        <center> 
            <h1 style="color:green;"> 
                GeeksforGeks 
            </h1> 
            <h2>HTML onplay Attribute</h2> 
  
            <video controls id="vidID" 
                   onplay="GFGfun()" 
                   width="320"
                   height="240"> 
                <source src= 
"https://media.geeksforgeeks.org/wp-content/uploads/20190401140735/g4g2.mp4" 
                        type="video/mp4" /> 
            </video> 
  
            <script> 
                function GFGfun() { 
                    alert("Video play"); 
                } 
            </script> 
        </center> 
    </body> 
</html>

輸出:

後:

支持的瀏覽器:

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




相關用法


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