目前我正在使用这样的代码
// Create the event.
var event = document.createEvent('Event');
// Define that the event name is 'swiping'.
event.initEvent('swiping', true, true);
// Listen for the event.
elem.addEventListener('swiping', function (e) {
// e.target matches elem
}, false);
// target can be any Element or other EventTarget.
elem.dispatchEvent(event);
处理元素滑动
我如何从HTML元素中触发此事件? (例如<div on-swiping="function-name"></div>
或PolymerJS自定义元素事件on-click
,on-tap
等)
答案 0 :(得分:1)
键elem.dispatchEvent(event);
您可以使用elem.addEventListener('build', function (e) {
当用户click
(或tab
)使用elem.dispatchEvent(event);
完整的演示是:
var elem = document.querySelector('#test');
// Listen for the event.
elem.addEventListener('build', function (e) {
alert(elem.innerText);
}, false);
document.querySelector('button').onclick = function() {
var event = new Event('build');
// Dispatch the event.
elem.dispatchEvent(event);
};
<div id="test">test</div>
<button>Buttons</button>