/ Published in: JavaScript
URL: http://www.gracecode.com/archives/2949/
Expand |
Embed | Plain Text
var isEventSupported = (function() { // 根据特有的事件创建对应的 HTML 元素 var TAGNAMES = { 'select':'input','change':'input', 'submit':'form','reset':'form', 'error':'img','load':'img','abort':'img' } function isEventSupported(eventName) { var el = document.createElement(TAGNAMES[eventName] || 'div'); eventName = 'on' + eventName; // 检测元素是否已经包含了对应的事件 var isSupported = (eventName in el); // 如果没有对应事件,则尝试增加对应事件,然后判断是否为回调 if (!isSupported) { el.setAttribute(eventName, 'return;'); isSupported = typeof el[eventName] == 'function'; } el = null; return isSupported; } return isEventSupported; })();
You need to login to post a comment.
