我有以下腳本,檢查我的計算機運行的是什麼版本的IE瀏覽器,並使用樣式表,以及顯示警報語句:爲什麼有不同版本的IE則不會觸發
var div = document.createElement("div");
div.innerHTML = "<!--[if lte IE 9]><i></i><![endif]-->";
var isIeLessThan9 = (div.getElementsByTagName("i").length == 1);
if (isIeLessThan9) {
alert("<=9");
document.write('<link rel="stylesheet" href="theStyles/defaultStyle_ie.css" type="text/css" charset="utf-8" />');
document.write('<link rel="stylesheet" href="theStyles/captionStyle_ie.css" type="text/css" charset="utf-8" />');
}
if (!isIeLessThan9) {
alert(">9");
document.write('<link rel="stylesheet" href="theStyles/defaultStyle.css" type="text/css" charset="utf-8" />');
document.write('<link rel="stylesheet" href="theStyles/captionStyle.css" type="text/css" charset="utf-8" />');
}
發生了什麼事是IE版本10及更高版本顯示>=10
和任何小於IE版本10仍顯示>=10
我該如何解決它?
編輯:
這是更新的代碼應該解決這個問題:
<script type="text/javascript">
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
navigator.sayswho= (function(){
var ua= navigator.userAgent, tem,
M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*([\d\.]+)/i) || [];
if(/trident/i.test(M[1])){
tem= /\brv[ :]+(\d+(\.\d+)?)/g.exec(ua) || [];
return 'IE '+(tem[1] || '');
}
M= M[2]? [M[1], M[2]]:[navigator.appName, navigator.appVersion, '-?'];
if((tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
return M.join(' ');
})();
var browserversion1 = navigator.sayswho.split(" ");
var browserversion2 = browserversion1[1].split(".")[0].split(",");
var isIeLessThan10 = browserversion1[0] == "IE" && browserversion2[0] < 10;
if (!isMobile.any()) {
if (isIeLessThan10) {
alert("IE<10");
document.write('<link rel="stylesheet" href="theStyles/defaultStyle_ie.css" type="text/css" charset="utf-8" />');
document.write('<link rel="stylesheet" href="theStyles/captionStyle_ie.css" type="text/css" charset="utf-8" />');
}
else {
alert("IE>=10 || !IE");
document.write('<link rel="stylesheet" href="theStyles/defaultStyle.css" type="text/css" charset="utf-8" />');
document.write('<link rel="stylesheet" href="theStyles/captionStyle.css" type="text/css" charset="utf-8" />');
}
}
else {
alert("mobile");
document.write('<link rel="stylesheet" href="theStyles/defaultStyle_mobile.css" type="text/css" charset="utf-8" />');
document.write('<link rel="stylesheet" href="theStyles/captionStyle_mobile.css" type="text/css" charset="utf-8" />');
}
</script>
雙方IE8和FF顯示警報,IE>=10 || !IE
如果'getElementsByTagName'不匹配任何元素,我希望它的'length'爲'0',而不是'-1'。 –
安東尼說什麼。如果返回的'NodeList'爲空,它的長度將爲0. – Ennui
**重要** _As Internet Explorer 10,標準mode_不再支持條件註釋。閱讀關於它的所有信息 - > http://msdn.microsoft.com/en-us/library/ms537512.ASPX – davidkonrad