当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。