var attrs = $('input').get(0).attributes;
var f = Array.prototype.filter.call(attrs, isEvent); //Filter all attributes
//f now contains all attribute nodes that are events
f.forEach(function(e) { console.log(e.nodeName); });
function isEvent(element) {
return element.nodeName.startsWith('on');
}
对于以下输入标记,它将记录 onclick、onchange。适用于附加的内联事件或使用 JavaScript 创建的事件属性。
<input name='student' onclick='show()' onchange='return true;'/>
或者
var el = $('input').get(0);
el.setAttribute('onkeyup', function() {});
getEventsFor(el).forEach(function(e) {
console.log(e.nodeName); //onSomeEvent
console.log(e.nodeValue); // attached handler
});
function isEvent(element) {
return element.nodeName.startsWith('on');
}
function getEventsFor(element) {
var attrs = element.attributes;
return Array.prototype.filter.call(attrs, isEvent);
}
JSFiddle