第一是使用HACK 即Conditional comments,格式为: <!--[if IE]>...<![endif]--> 详情见 http://zhanjia.iteye.com/blog/369023
这种写法只有IE能够识别,就是说这种方法只能用来判断IE版本(当然对于加载样式表这件事已经足够了)。
第二种是通过JavaScript判断浏览器类型,然后在页面动态生成<link>元素。好处是可以判断各种不同的浏览器类型。
具体就是通过
1. navigator.userAgent[.toLowerCase()] 得到当前客户端的名称 然后用正则test一下;
2. document.createElement('link') appendChild到head或者body
代码如下var userAgent = funciton() {//此处省略500字}
if(userAgent .isIE()) {
var style = document.createElement('link');
style.rel = 'stylesheet';
style.type = 'text/css';
style.href = '***'; // css的路径
document.body.appendChild(style);
}
本文介绍了两种用于判断浏览器类型并加载样式表的方法:使用IE条件注释(Conditional comments)和JavaScript。通过这两种方法,开发者可以根据浏览器的不同特性,实现更加灵活的网页适配。
207

被折叠的 条评论
为什么被折叠?



