1.1 getElementById区别
<select name="month">
<option>Jan</option>
<opton>Feb</option>
</select>
<select name="month">
<option>Jan</option>
<opton>Feb</option>
</select>
IE中:
document.getElementById("month")正确
document.getElementsByName("month")正确
Firefox:
document.getElementById("month")错误(必须加属性Id)
document.getElementsByName("month")正确
故通用方法编写标准html语法,然后用document.getElementById.
1.2 FireFox不支持vbscrīpt
1.3 Firefox定位form
Form定义 <form name="userlist" id="userlist" method="post" action="userlist.do">
仅仅IE支持直接访问:
userlist.pageSize.value="20";
userlist.submit();
通用函数
function goto(p) {
document.forms["userlist"].page.value=p;
document.forms["userlist"].pageSize.value="20";
document.forms["userlist"].submit();
}
1.4 事件函数
仅仅IE支持document.onclick(){这种方式书写。
function document.onclick(){
with(window.event.srcElement){
if (tagName != "INPUT" && getAttribute("Author") != "tiannet")
tiannetHideControl();
}
}
在FireFox上提示:missing ( before formal parameters,javascrīpt error
1.5 Console关键字
<scrīpt type='text/javascrīpt'>
var http_request=null;
alert(console);
var console=null;
</scrīpt>
在firefox上console为一个对象
setting a property that has only a getter
IE表现为未定义
1.6 事件处理
http://www.phpchina.com/12834/viewspace_29308.html
http://www.javaeye.com/topic/47917
如:
function getEvent(){ //同时兼容ie和ff的写法
if(document.all) return window.event;
func=getEvent.caller;
while(func!=null){
var arg0=func.arguments[0];
if(arg0){
if((arg0.constructor==Event || arg0.constructor ==MouseEvent)
|| (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){
return arg0;
}
}
func=func.caller;
}
return null;
}
//任意点击时关闭该控件
document.onclick=function(){
var ōbjevent= getEvent(); //window.event ;//getEvent();
if( typeof(objevent.srcElement) == "undefined")
{
with(objevent.target)
{
if (tagName != "INPUT" && getAttribute("Author") != "tiannet")
{
tiannetHideControl();
}
}
}
else
{
with(objevent.srcElement)
{
if (tagName != "INPUT" && getAttribute("Author") != "tiannet")
{
tiannetHideControl();
}
}
}
}
1.7 Document.all
IE特有:
document.all.selTianMonth
通用
document.getElementById('selTianMonth')
1.8 innerText
http://www.cnlei.org/blog/article.asp?id=425
为Firefox下的DOM对象增加innerText属性:
<scrīpt type="text/javascrīpt">
var lBrowser = {};
lBrowser.agt = navigator.userAgent.toLowerCase();
lBrowser.isW3C= document.getElementById ? true:false;
lBrowser.isIE = ((lBrowser.agt.indexOf("msie") != -1) && (lBrowser.agt.indexOf("opera") == -1) && (lBrowser.agt.indexOf("omniweb") == -1));
lBrowser.isNS6 = lBrowser.isW3C&& (navigator.appName=="Netscape") ;
lBrowser.isOpera = lBrowser.agt.indexOf("opera") != -1;
lBrowser.isGecko = lBrowser.agt.indexOf("gecko") != -1;
lBrowser.ieTrueBody =function (){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
};
//为Firefox下的DOM对象增加innerText属性
if(lBrowser.isNS6){ //firefox innerText define
HTMLElement.prototype.__defineGetter__( "innerText",
function(){
return this.textContent;
}
);
HTMLElement.prototype.__defineSetter__( "innerText",
function(sText){
this.textContent=sText;
}
);
}
alert(lBrowser.isNS6);
</scrīpt>
1.9 eval
创建过程:
for (var i=0;i<5;i++) {
document.write (' <tr align=center id="trTiannetDay' + i + '" >');
for (var j=0;j<7;j++){
document.write('<td align="center" id="tdTiannetDay' + n + '" '+
'onClick="tiannetDay=this.innerText;tiannetSetValue(true);" '
+' style="' + s_tiannet_day + '"> </td>');
n ++;
}
document.write (' </tr>');
}
IE特有:
for (var i = 0; i < 35; i++){
eval("document.all.tdTiannetDay"+i);
通用:
var ōbjname="tdTiannetDay"+ i;
var da = eval( document.getElementById(objname));
if (typeof(da) == undefined)
{
alert("document.getElementById(tdTiannetDay"+i+"is null");
}
else
{
alert(da.tagName);
//alert(typeof(da.constructor));
}
查找到一个区别:http://kongjian.baidu.com/freezesoul/blog/item/f6b7e5cd275752510eb34548.html
javascrīpt中dom对象和普通对象可以通过判断tagName属性来区别,但是这不是非常好的办法,因为你很难禁止他人在普通对象中定义tagName属性。另外一个比较好的方法是dom对象的constructor属性一般为undefined(同时,显然地,instanceof Object将返回false,这是它们区别其他对象的一个明显特征
1.10 execscrīpt
如:window.execscrīpt("c = Asc(c)", "vbscrīpt");
仅仅IE支持