Posted By

mifly on 04/10/09


Tagged

event


Versions (?)

event test


 / Published in: JavaScript
 

URL: http://www.gracecode.com/archives/2949/

  1. var isEventSupported = (function() {
  2. // 根据特有的事件创建对应的 HTML 元素
  3. var TAGNAMES = {
  4. 'select':'input','change':'input',
  5. 'submit':'form','reset':'form',
  6. 'error':'img','load':'img','abort':'img'
  7. }
  8.  
  9. function isEventSupported(eventName) {
  10. var el = document.createElement(TAGNAMES[eventName] || 'div');
  11. eventName = 'on' + eventName;
  12.  
  13. // 检测元素是否已经包含了对应的事件
  14. var isSupported = (eventName in el);
  15.  
  16. // 如果没有对应事件,则尝试增加对应事件,然后判断是否为回调
  17. if (!isSupported) {
  18. el.setAttribute(eventName, 'return;');
  19. isSupported = typeof el[eventName] == 'function';
  20. }
  21. el = null;
  22. return isSupported;
  23. }
  24. return isEventSupported;
  25. })();

Report this snippet  

You need to login to post a comment.