栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Javascript IE检测,为什么不使用简单的条件注释?[重复]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Javascript IE检测,为什么不使用简单的条件注释?[重复]

James Padolsey在GitHub上放了一些代码片段,我将在这里引用:

// ----------------------------------------------------------// A short snippet for detecting versions of IE in Javascript// without resorting to user-agent sniffing// ----------------------------------------------------------// If you're not in IE (or IE version is less than 5) then:// ie === undefined// If you're in IE (>=5) then you can determine which version:// ie === 7; // IE7// Thus, to detect IE:// if (ie) {}// And to detect the version:// ie === 6 // IE6// ie > 7 // IE8, IE9 ...// ie < 9 // Anything less than IE9// ----------------------------------------------------------// UPDATe: Now using Live NodeList idea from @jdaltonvar ie = (function(){    var undef,        v = 3,        div = document.createElement('div'),        all = div.getElementsByTagName('i');    while (        div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',        all[0]    );    return v > 4 ? v : undef;}());

当然,所有的荣誉都应该归詹姆斯所有,我只是信使(但如果我的复制粘贴操作有误,请射击信使)。

还要看看创建的叉子。保罗·爱尔兰(PaulIrish)在评论中解释了内部工作原理。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/464817.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号