首页 > 技术文章 > 【前端学习】检测浏览器是否支持这些属性

lomo321 2015-03-31 17:49 原文

  1. var supports = (function() {
  2. var div = document.createElement('div'),
  3. vendors = 'Khtml Ms O Moz Webkit'.split(' '),
  4. len = vendors.length;
  5.  
  6. return function(prop) {
  7. if ( prop in div.style ) return true;
  8.  
  9. prop = prop.replace(/^[a-z]/, function(val) {
  10. return val.toUpperCase();
  11. });
  12.  
  13. while(len--) {
  14. if ( vendors[len] + prop in div.style ) {
  15. // browser supports box-shadow. Do what you need.
  16. // Or use a bang (!) to test if the browser doesn't.
  17. return true;
  18. }
  19. }
  20. return false;
  21. };
  22. })();
  23.  
  24. if ( supports('textShadow') ) {
  25. document.documentElement.className += ' textShadow';
  26. }

推荐阅读