--- title: GlobalEventHandlers.onplay slug: Web/API/GlobalEventHandlers/onplay translation_of: Web/API/GlobalEventHandlers/onplay ---
{{ ApiRef("HTML DOM") }}

The onplay property of the {{domxref("GlobalEventHandlers")}} mixin is the {{event("Event_handlers", "event handler")}} for processing {{event("play")}} events.

语法

element.onplay = handlerFunction;
var handlerFunction = element.onplay;

handlerFunction 应该是 null 或者 JavaScript function 指定事件的处理程序。

示例

<p>此示例演示如何将“onplay”事件分配给视频元素</p>

<video controls onplay="alertPlay()">
  <source src="mov_bbb.mp4" type="video/mp4">
  <source src="mov_bbb.ogg" type="video/ogg">
  Your browser does not support HTML5 video.
</video>

<p>Video courtesy of <a href="http://www.bigbuckbunny.org/" target="_blank">Big Buck Bunny</a>.</p>

<script>
function alertPlay() {
  alert("视频开始播放啦。");
}
</script>

规范

Specification Status Comment
{{SpecName('HTML WHATWG','webappapis.html#handler-onplay','onplay')}} {{Spec2('HTML WHATWG')}}

 

浏览器兼容性

{{Compat("api.GlobalEventHandlers.onplay")}}

相关知识