纯css方式
以下是引用片段: Code .box{ width: 20em; height:20em; background: #369; } /*--gecko内核--*/ @media all and (min-width: 0px){ box{ background: #CC0000; } } /*--opera hacks--*/ /*--不能通过W3C css检验--*/ <!--[if IE ]><style> .box{ background: #808080; } </style><![endif]--> |
以上代码运行的结果,在IE中是灰色,Opera中是紫红,Firefox中是蓝色
在IE7以前版本还可采用css2的属性选择符来区别,不幸的是IE7开始已能识别。
本文章由forestgan于15-Oct-2006发表
javascript的方法
以下是引用片段: var css_browser_selector = function() { var ua = navigator.userAgent.toLowerCase(), is = function(t){ return ua.indexOf(t) != -1; }, h = document.getElementsByTagName('html')[0], b = (!(/opera|webtv/i.test(ua)) && /msie (d)/.test(ua)) ? ((is('mac') ? 'ieMac ' : '') + 'ie ie' + RegExp. $1) : is('gecko/') ? 'gecko' : is('opera') ? 'opera' : is('konqueror') ? 'konqueror' : is('applewebkit/') ? 'webkit safari' : is('mozilla/') ? 'gecko' : '', os = (is('x11') || is('linux')) ? ' linux' : is('mac') ? ' mac' : is('win') ? ' win' : ''; var c = b+os+' js'; h.className += h.className?' '+c:c; }(); |