2013-01-23 50 views
0

在我的页面中,我使用JavaScript功能块与条件。如果浏览器是IE8,那么样式将如此,或者在其他样式中使用样式。但是,如果我使用下面的条件,该功能不起作用。请人帮我解决这个问题检查功能内的浏览器类型不工作在Javascript

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);"> 
+0

究竟错在何处? IE变量来自哪里? –

+0

var isOpera,isIE = false;如果(typeof(window.opera)!='undefined'){isOpera = true;} if(!isOpera && navigator.userAgent.indexOf('Internet Explorer')){isIE = true);在页面的顶部 – Rithu

+0

但我有一个JavaScript函数,其中包含包含多个变量的长编码。例如http://jsfiddle.net/3Sd4W/ 这是我的代码,我必须更改每个支持IE8和Chrome的变量。任何想法。在那个addnew函数中,IE8不支持onclick,class,setattribute和其他一些东西 – Rithu

回答

2

有目标的Internet Explorer的特定版本更清洁的方式,Conditional Comments.

例子:

<!--[if IE 8]> 
<style type="text/css"> 

.my-div-class-here { 
    width:110px; 
    height:80px; 
    ... } 

</style> 
<![endif]--> 
0
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 
} 
相关问题