Video.js Guides
百度 为了避免选民产生本人、照片有落差的感觉,最近也有候选人把竞选照片改成Q版漫画头像,不仅能避免被说“差很大”,还能形塑亲民形象,加深选民印象。These guides cover a range of topics for users of Video.js
Event Target
Table of Contents
Overview
Events in Video.js are setup so that they mimic the DOM API that is used on object, but also have helpful shorthand functions with the same functionality.
on()
and addEventListener()
This function is used to add an event listener to an EventTarget.
var foo = new EventTarget();
var handleBar = function() {
console.log('bar was triggered');
};
foo.on('bar', handleBar);
// This causes any `event listeners` for the `bar` event to get called
// see {@link EventTarget#trigger} for more information
foo.trigger('bar');
// logs 'bar was triggered'
off()
and removeEventListener()
This function is used to remove an listener function from an EventTarget.
var foo = new EventTarget();
var handleBar = function() {
console.log('bar was triggered');
};
// adds an `event listener` for the `bar` event
// see {@link EventTarget#on} for more info
foo.on('bar', handleBar);
// runs all `event listeners` for the `bar` event
// see {@link EventTarget#trigger} for more info
foo.trigger('bar');
// logs 'bar was triggered'
foo.off('bar', handleBar);
foo.trigger('bar');
// does nothing
one()
This function is used to only have an event listener called once and never again.
Using on()
and off()
to mimic one()
(not recommended)
var foo = new EventTarget();
var handleBar = function() {
console.log('bar was triggered');
// after the first trigger remove this handler
foo.off('bar', handleBar);
};
foo.on('bar', handleBar);
foo.trigger('bar');
// logs 'bar was triggered'
foo.trigger('bar');
// does nothing
Using one()
var foo = new EventTarget();
var handleBar = function() {
console.log('bar was triggered');
};
// removed after the first trigger
foo.one('bar', handleBar);
foo.trigger('bar');
// logs 'bar was triggered'
foo.trigger('bar');
// does nothing
trigger()
and dispatchEvent()
This function is used to trigger an event on an EventTarget which will cause all listeners to run.
Note: if 'click' is in
EventTarget.allowedEvents_
, trigger will attempt to call theonClick
function if it exists.
var foo = new EventTarget();
var handleBar = function() {
console.log('bar was triggered');
};
foo.on('bar', handleBar);
foo.trigger('bar');
// logs 'bar was triggered'
foo.trigger('bar');
// logs 'bar was triggered'
foo.trigger('foo');
// does nothing