您的位置:首页 > Web前端 > JavaScript

JavaScript Var 'Undefined' in Firefox; IE and Opera Work fine

2011-01-14 16:28 597 查看
I have some JavaScript that works fine in IE and Opera, but not Firefox.

The issue seems to be that Firefox is seeing the variable as undefined.

Do I have to declare that variable even though it should be inherited?

Your problem is that you're using document.all, and .innerText, neither of which are standard.

You should be using document.getElementById() to grab items, and .innerHTML to read their contents, if they're not form inputs.

If they are inputs, use .value instead of .innerText

Firfox支持:document.getElementById("c0").textContent
IE支持: document.getElementById("c0").innerText
<mce:script   language="javascript"   ><!--
function   isIE(){
//alert("")
//alert(window.navigator.userAgent)
//alert(window.navigator.userAgent.toString().toLowerCase().indexOf("msie"))
if   (window.navigator.userAgent.toString().toLowerCase().indexOf("msie") >=1)
return   true;
else
return   false;
}
//alert(isIE())

if(!isIE()){   //firefox   innerText   define
HTMLElement.prototype.__defineGetter__(           "innerText",
function(){
var   anyString   =   "";
var   childS   =   this.childNodes;
for(var   i=0;   i <childS.length;   i++)   {
if(childS[i].nodeType==1)
anyString   +=   childS[i].tagName=="BR"   ?   '/n'   :   childS[i].innerText;
else   if(childS[i].nodeType==3)
anyString   +=   childS[i].nodeValue;
}
return   anyString;
}
);
HTMLElement.prototype.__defineSetter__(           "innerText",
function(sText){
this.textContent=sText;
}
);
}
alert(document.getElementById("aaaa").textContent)
alert(document.getElementById("aaaa").innerText)
// --></mce:script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐