There is a cleaner way to target specific versions of Internet Explorer, Conditional Comments.
Example:
<!--[if IE 8]>
<style type="text/css">
.my-div-class-here {
width:110px;
height:80px;
... }
</style>
<![endif]-->
Вопрос
In my page, I used javascript function for block with a condition. If the browser is IE8 then the style will be like this or use style within else. But if I used the below condition, the function not works. Anyone please help me to solve this issue
function addnew(type)
{
type=parseInt(type)+1;
var isOpera, isIE = false;
if(typeof(window.opera) != 'undefined'){isOpera = true;}
if(!isOpera && navigator.userAgent.indexOf('Internet Explorer')){isIE = true);
var mydiv = document.createElement("div");
mydiv.setAttribute("id",name5);
if(!IE){
mydiv.setAttribute("style","width:110px;height:80px;background-image:url(images/transparent.png);float:left;text-align:center;border:1px solid #ccc;");
}
else
{
mydiv.style.setAttribute('cssText', "width:110px;height:80px;background-image:url(images/transparent.png);float:left;text-align:center;border:1px solid #ccc;");
}
}
<input id="addchoice" type=button value="Add New Entry" onclick="addnew(document.forms['addpoll']['choicecount'].value);">
Решение
There is a cleaner way to target specific versions of Internet Explorer, Conditional Comments.
Example:
<!--[if IE 8]>
<style type="text/css">
.my-div-class-here {
width:110px;
height:80px;
... }
</style>
<![endif]-->
Другие советы
function addnew(type)
{
var isOpera, isIE = false;
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
if (ieversion==8)
isIE = true;
}
if(isIE)
{
//code for IE
}
else
{
//code for other browsers
}