W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
本文演示如何創(chuàng)建和分派DOM事件。此類事件通常稱為合成事件,而不是瀏覽器本身觸發(fā)的事件。
可以使用Event
構造函數(shù)創(chuàng)建事件,如下所示:
var event = new Event('build');
// Listen for the event.
elem.addEventListener('build', function (e) { /* ... */ }, false);
// Dispatch the event.
elem.dispatchEvent(event);
上面的代碼示例使用EventTarget.dispatchEvent()方法。
大多數(shù)現(xiàn)代瀏覽器都支持此構造函數(shù)(Internet Explorer是例外)。有關更詳細的方法(適用于Internet Explorer),請參閱下面的老式方法。
要向事件對象添加更多數(shù)據(jù),存在CustomEvent接口,detail屬性可用于傳遞自定義數(shù)據(jù)。
例如,可以按如下方式創(chuàng)建事件:
var event = new CustomEvent('build', { detail: elem.dataset.time });
這將允許您訪問事件偵聽器中的其他數(shù)據(jù):
function eventHandler(e) {
console.log('The time is: ' + e.detail);
}
較舊的創(chuàng)建事件的方法使用受Java啟發(fā)的API。以下是一個示例:
// Create the event.
var event = document.createEvent('Event');
// Define that the event name is 'build'.
event.initEvent('build', true, true);
// Listen for the event.
elem.addEventListener('build', function (e) {
// e.target matches elem
}, false);
// target can be any Element or other EventTarget.
elem.dispatchEvent(event);
通常需要從子元素觸發(fā)事件,并讓祖先捕獲它;可選地,使用數(shù)據(jù):
<form>
<textarea></textarea>
</form>
const form = document.querySelector('form');
const textarea = document.querySelector('textarea');
// Create a new event, allow bubbling, and provide any data you want to pass to the "details" property
const eventAwesome = new CustomEvent('awesome', {
bubbles: true,
detail: { text: () => textarea.value }
});
// The form element listens for the custom "awesome" event and then consoles the output of the passed text() method
form.addEventListener('awesome', e => console.log(e.detail.text()));
// As the user types, the textarea inside the form dispatches/triggers the event to fire, and uses itself as the starting point
textarea.addEventListener('input', e => e.target.dispatchEvent(eventAwesome));
元素可以偵聽尚未創(chuàng)建的事件:
<form>
<textarea></textarea>
</form>
const form = document.querySelector('form');
const textarea = document.querySelector('textarea');
form.addEventListener('awesome', e => console.log(e.detail.text()));
textarea.addEventListener('input', function() {
// Create and dispatch/trigger an event on the fly
// Note: Optionally, we've also leveraged the "function expression" (instead of the "arrow function expression") so "this" will represent the element
this.dispatchEvent(new CustomEvent('awesome', { bubbles: true, detail: { text: () => textarea.value } }))
});
此示例演示如何使用DOM方法模擬復選框上的單擊(以編程方式生成單擊事件)。
function simulateClick() {
var event = new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true
});
var cb = document.getElementById('checkbox');
var cancelled = !cb.dispatchEvent(event);
if (cancelled) {
// A handler called preventDefault.
alert("cancelled");
} else {
// None of the handlers called preventDefault.
alert("not cancelled");
}
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: