我试图找到哪个版本的IE用户,并根据哪个浏览器向body标签添加一个类。
我的代码是
if (navigator.appName == "Microsoft Internet Explorer") {
//Set IE as true
ie = true;
//Create a user agent var
var ua = navigator.userAgent;
//Write a new regEx to find the version number
var re = new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})");
//If the regEx through the userAgent is not null
if (re.exec(ua) != null) {
//Set the IE version
ieVersion = parseInt(RegExp.$1);
}
}
else {
ie = false;
}
function ieTag() {
if (ie == true) {
if (ieVersion == 7) {
$('body').addClass('IE7');
}
}
if (ie == true) {
if (ieVersion == 8) {
$('body').addClass('IE8');
}
}
if (ie == true) {
if (ieVersion == 9) {
$('body').addClass('IE9');
}
}
}
我正在使用它来调用函数
<script type="text/javascript">
$(document).ready(function () {
//IE Version Control
ieTag();
});
</script>
但是由于某些原因我只是拿起IE 9,我之前有这个脚本工作所以我真的不明白什么是错的!!!
我甚至尝试过使用这个脚本function ieTag() {
if (ie == true) {
$('body').addClass('IE' + ieVersion);
}
}
但仍然只是拿起IE9
我使用IE(和开发人员工具来更改版本(以前这两个脚本都已经使用过)
答案 0 :(得分:8)
这可能不是您正在寻找的答案,但它似乎是最简单的解决方案:
<!--[if lt IE 7]> <body class="ie6"> <![endif]-->
<!--[if IE 7]> <body class="ie7"> <![endif]-->
<!--[if IE 8]> <body class="ie8"> <![endif]-->
<!--[if IE 9]> <body class="ie9"> <![endif]-->
<!--[if gt IE 9]> <body class="ie10+"> <![endif]-->
<!--[if !IE]><!--> <body> <!--<![endif]-->
当然在HTML中添加了你的body标签。
答案 1 :(得分:1)
您可以使用条件注释,以免影响其他浏览器。
<!--[if IE 6]>
<script>
$(document).ready(function(){
$("body").addClass("ie-6");
});
</script>
<![endif]-->
<!--[if IE 7]>
<script>
$(document).ready(function(){
$("body").addClass("ie-7");
});
</script>
<![endif]-->
<!--[if IE 8]>
<script>
$(document).ready(function(){
$("body").addClass("ie-8");
});
</script>
<![endif]-->
<!--[if IE 9]>
<script>
$(document).ready(function(){
$("body").addClass("ie-9");
});
</script>
<![endif]-->
答案 2 :(得分:0)
您不需要JavaScript。
<!--[if IE 9]>
<body class='ie9'>
<![endif]-->
<!--[if IE 8]>
<body class='ie8'>
<![endif]-->
等。对于普通浏览器,您可以:
<!--[if IE]><!-->
<body class='normal'>
<!--<![endif]-->
答案 3 :(得分:0)
问题中的js在IE 7和IE 8中失败的原因是我使用application / javascript而不是text / javascript包含脚本的事实,
原因是不适用于application / javascript是因为这是一种新的方式,包括只有现代浏览器支持的脚本和旧浏览器不支持这种方法,因此IE 7&amp; 8失败。
答案 4 :(得分:0)
如果你有jQuery,你可以像这样添加ie类到身体
$(function(){
if ($.browser.msie && $.browser.version == 8) {
$('body').addClass('ie8');
}
});