js+dom中在不同浏览器的区别

阅读:8088回复:9
2005-05-18 18:01
写私信
楼主#
我这段时间都在写js+dom的代码,不知道有什么实用一点的文章。

如果可以的话,大家都可以把自己知道的区别写上来,让我们可以写出兼容性好的代码。

我先写几个,如果有新的我就加在后面。

1. childNodes在ff中和ie的区别。
   ff中的node(nodeType = 1)都是用textNode(nodeType = 3)分开的,而ie/op不是这样的。
   <div id="box1"><span>content</span></div>
   在ff下,box1的childNodes为3个,ie下为1个。


2. 设置某个node对象的style class名称。
   ie中要设置某个node的class用"className"作为attr来set或者get。
   ff等其它的浏览器用"class"作为attr来set或者get。
  
if(typeof node1.getAttribute("className") == "string") {
      ....
   }


3. 设置某个node对象的style content。
    直接举例把
  
var oStyle = oNode.getAttribute("style");
                // ie
		if(oStyle == "[object]") {
			oStyle.setAttribute("cssText", strStyle);
			oNode.setAttribute("style", oStyle);
		} else {
			oNode.setAttribute("style", strStyle);
		}

  
4. 事件对象。
    ie用event
    ff用evnt

5. 事件作用对象
    ie用objEvent.srcElement
    ff用objEvent.target


大家有新的就补充一下吧,我目前写得js代码不多,就这点内容。
希望这个帖子对我这样的初级开发者能实用~  
关注WebStandard,关心那些不使用IE的弱势群体!
2005-05-18 18:01
写私信
1楼#
写得不错,如果再能和W3C DOM做一下对比就更好了。
还有,这个说得不太清楚准确,

4. 事件对象。
ie用event
ff用evnt

希望能改进补充一下。
2005-05-18 18:01
写私信
2楼#
真是够混乱的。ie 6.0|opera 8.01|firefox 1.0+20050323

<p style="color:#fff;background:#780e1a;">Test for set style.</p>
<button onclick="document.getElementsByTagName('p')[0].style='color:#000;background:#eee;'">set style(op only)</button>
<button onclick="document.getElementsByTagName('p')[0].style.cssText='color:#000;background:#eee;'">set style(ff & ie)</button>
<button onclick="document.getElementsByTagName('p')[0].setAttribute('style','color:#000;background:#eee;')">set style(ff & op)</button>
<button onclick="document.getElementsByTagName('p')[0].getAttribute('style').setAttribute('cssText','color:#000;background:#eee;')">set style(ie only)</button>
2005-05-18 18:01
写私信
3楼#
顺便请教一下,gecko 会get 出来计算过的样式表,而我只需要得到 "background:#ff0000" 字符串怎么办?

<style>div {margin-top:1px;}</style>
<div style="background:#ff0000" onclick="alert(this.style.backgroundColor)">test</div>
<div style="background:#ff0000" onclick="alert(this.getAttributeNode('style').value)">test</div>
<div style="background:#ff0000" onclick="alert(this.getAttribute('style'))">test</div>
<div style="background:#ff0000" guo="guoshuang" onclick="alert(this.getAttribute('guo'))">test</div>
2005-05-18 18:01
写私信
4楼#
guoshuang:顺便请教一下,gecko 会get 出来计算过的样式表,而我只需要得到 "background:#ff0000" 字符串怎么办?

<style>div {margin-top:1px;}</style>
<div style="background:#ff0000" onclick="alert(this.style.backgroundColor)">test</div>
<div style="background:#ff0000" onclick="alert(this.getAttributeNode('style').value)">test</div>
<div style="background:#ff0000" onclick="alert(this.getAttribute('style'))">test</div>
<div style="background:#ff0000" guo="guoshuang" onclick="alert(this.getAttribute('guo'))">test</div>
回到原帖


试下把style当attribute来getAttribute一下?
2005-05-18 18:01
写私信
5楼#
jhsea3do:1. childNodes在ff中和ie的区别。
   ff中的node(nodeType = 1)都是用textNode(nodeType = 3)分开的,而ie/op不是这样的。
   <div id="box1"><span>content</span></div>
   在ff下,box1的childNodes为3个,ie下为1个。
回到原帖

这个跟 xml 文件写作有关,将 IE 的 preserveWhiteSpace 设为 true 看看,底下是取自微软的说明文件
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmlDoc.async = false;
xmlDoc.preserveWhiteSpace = true;
xmlDoc.load("books.xml");
alert(xmlDoc.xml);
xmlDoc.async = false;
xmlDoc.preserveWhiteSpace = false;
xmlDoc.load("books.xml");
alert(xmlDoc.xml);
2005-05-18 18:01
写私信
6楼#
to 猫头猪:不是很理解。下面的也不行,:(

<div style="background:#ff0000" onclick="alert(this.attributes[1].value)">test</div>

to softcup:You are so cool.Thanks.
2005-05-18 18:01
写私信
7楼#
还真是不行啊,试了cssText都是计算过的。
2005-05-18 18:01
写私信
8楼#
谢谢说明,我对ie的xml开发包不熟悉。

softcup
这个跟 xml 文件写作有关,将 IE 的 preserveWhiteSpace 设为 true 看看,底下是取自微软的说明文件
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmlDoc.async = false;
xmlDoc.preserveWhiteSpace = true;
xmlDoc.load("books.xml");
alert(xmlDoc.xml);
xmlDoc.async = false;
xmlDoc.preserveWhiteSpace = false;
xmlDoc.load("books.xml");
alert(xmlDoc.xml);
回到原帖
关注WebStandard,关心那些不使用IE的弱势群体!
2005-05-18 18:01
写私信
9楼#
猫头猪:写得不错,如果再能和W3C DOM做一下对比就更好了。
还有,这个说得不太清楚准确,

希望能改进补充一下。
回到原帖


谢谢斑竹加精了,这个帖子我会努力完善的。只是这几天非常繁忙,来不及整理!
关注WebStandard,关心那些不使用IE的弱势群体!