1.dom上的接口基本上还是一致的,但经测试发现mozilla下的dom更标准些,就算些法一样,ie下会有一些微小的区别,但无关要紧
2.事件模型上,这方面区别算比较大.
mozilla下的e.target 相当于 ie下的event.srcelement,但细节上有区别,后者是返回一个html element
而e.target返回的是个节点,也就是说包括文本节点,方法可以这样
var trg = e.target;
while(trg.nodetype!=1)trg=trg.parentnode;
mozilla下的e.which与ie下的event.keycode相当
相对应的还有e.layerx,e.layery,e.pagex,e.pagey...
可以看看http://fason.nease.net/mozilla/dom/ event部份
事件绑定上mozilla用addeventlistener,removeeventlistener,对应ie的attachevent,detatchevent
3.对象引用上就直接document.getelementbyid就行了,如果还要兼容ie4,可以再加上document.all判断
form element的引用要标准些var oinput = document.formname.elements[input1]
4.xml的应用上区别更大些,因为ie下是通过activex来创建,而mozilla已经是有这些对象的(需要dom2支持)
xmldomdocument: var doc = document.inplementation.createdocument(,,null)
xmlhttp: var req = new xmlhttprequest()
5.innertext就在mozilla不支持了,需要用些range的技巧来取得它的text
6.insertadjacenthtml是个比较好用的方法,mozilla可以用dom的方法insertbefore来兼容
7.更细微的,如array,date的一些方法ie和mozilla也会有一些微小的区别,具体不提到了。。。
写了两个例子:
1. 对于通过id取对象
function getobjectbyid(id)
{
if (typeof(id) != string || id == ) return null;
if (document.all) return document.all(id);
if (document.getelementbyid) return document.getelementbyid(id);
try {return eval(id);} catch(e){ return null;}
}
2. 对事件附加处理函数
if(document.attachevent)
window.attachevent(onresize, function(){reinsert();});
else
window.addeventlistener('resize', function(){reinsert();}, false);
注意在ie里是 onclick 而在firefox ns 里则是 click
用脚本提交
document.formname.action = ...;
document.formname.submit();
好像在mozilla下不能用
处理xml的方法
复制代码 代码如下:
var fckxml = function()
{}
fckxml.prototype.gethttprequest = function()
{
if ( window.xmlhttprequest )// gecko
return new xmlhttprequest() ;
else if ( window.activexobject )// ie
return new activexobject(msxml2.xmlhttp) ;
}
fckxml.prototype.loadurl = function( urltocall, asyncfunctionpointer )
{
var ofckxml = this ;
var basync = ( typeof(asyncfunctionpointer) == 'function' ) ;
var oxmlhttp = this.gethttprequest() ;
oxmlhttp.open( get, urltocall, basync ) ;
if ( basync )
{
oxmlhttp.onreadystatechange = function()
{
if ( oxmlhttp.readystate == 4 )
{
ofckxml.domdocument = oxmlhttp.responsexml ;
asyncfunctionpointer( ofckxml ) ;
}
}
}
oxmlhttp.send( null ) ;
if ( ! basync && oxmlhttp.status && oxmlhttp.status == 200 )
this.domdocument = oxmlhttp.responsexml ;
else
throw( 'error loading ' + urltocall + '' ) ;
}
fckxml.prototype.selectnodes = function( xpath, contextnode )
{
if ( document.all )// ie
{
if ( contextnode )
return contextnode.selectnodes( xpath ) ;
else
return this.domdocument.selectnodes( xpath ) ;
}
else// gecko
{
var anodearray = new array();
var xpathresult = this.domdocument.evaluate( xpath, contextnode ? contextnode : this.domdocument,
this.domdocument.creatensresolver(this.domdocument.documentelement), xpathresult.ordered_node_iterator_type, null) ;
if ( xpathresult )
{
var onode = xpathresult.iteratenext() ;
while( onode )
{
anodearray[anodearray.length] = onode ;
onode = xpathresult.iteratenext();
}
}
return anodearray ;
}
}
fckxml.prototype.selectsinglenode = function( xpath, contextnode )
{
if ( document.all )// ie
{
if ( contextnode )
return contextnode.selectsinglenode( xpath ) ;
else
return this.domdocument.selectsinglenode( xpath ) ;
}
else// gecko
{
var xpathresult = this.domdocument.evaluate( xpath, contextnode ? contextnode : this.domdocument,
this.domdocument.creatensresolver(this.domdocument.documentelement), 9, null);
if ( xpathresult && xpathresult.singlenodevalue )
return xpathresult.singlenodevalue ;
else
return null ;
}
}