jhsea3do
小狐狸
小狐狸
  • UID5719
  • 注册日期2005-05-05
  • 最后登录2006-01-18
  • 发帖数45
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
阅读:8014回复:9

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

楼主#
更多 发布于: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的弱势群体!
猫头猪
狐狸大王
狐狸大王
  • UID163
  • 注册日期2004-11-25
  • 最后登录2005-12-04
  • 发帖数595
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
1楼#
发布于:2005-05-18 18:01
写得不错,如果再能和W3C DOM做一下对比就更好了。
还有,这个说得不太清楚准确,

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

希望能改进补充一下。
guoshuang
火狐狸
火狐狸
  • UID771
  • 注册日期2004-12-11
  • 最后登录2006-12-28
  • 发帖数134
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
2楼#
发布于:2005-05-18 18:01
真是够混乱的。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>
guoshuang
火狐狸
火狐狸
  • UID771
  • 注册日期2004-12-11
  • 最后登录2006-12-28
  • 发帖数134
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
3楼#
发布于:2005-05-18 18:01
顺便请教一下,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>
猫头猪
狐狸大王
狐狸大王
  • UID163
  • 注册日期2004-11-25
  • 最后登录2005-12-04
  • 发帖数595
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
4楼#
发布于:2005-05-18 18:01
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一下?
softcup
小狐狸
小狐狸
  • UID5015
  • 注册日期2005-04-12
  • 最后登录2006-06-01
  • 发帖数6
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
5楼#
发布于:2005-05-18 18:01
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);
guoshuang
火狐狸
火狐狸
  • UID771
  • 注册日期2004-12-11
  • 最后登录2006-12-28
  • 发帖数134
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
6楼#
发布于:2005-05-18 18:01
to 猫头猪:不是很理解。下面的也不行,:(

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

to softcup:You are so cool.Thanks.
猫头猪
狐狸大王
狐狸大王
  • UID163
  • 注册日期2004-11-25
  • 最后登录2005-12-04
  • 发帖数595
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
7楼#
发布于:2005-05-18 18:01
还真是不行啊,试了cssText都是计算过的。
jhsea3do
小狐狸
小狐狸
  • UID5719
  • 注册日期2005-05-05
  • 最后登录2006-01-18
  • 发帖数45
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
8楼#
发布于:2005-05-18 18:01
谢谢说明,我对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的弱势群体!
jhsea3do
小狐狸
小狐狸
  • UID5719
  • 注册日期2005-05-05
  • 最后登录2006-01-18
  • 发帖数45
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
9楼#
发布于:2005-05-18 18:01
猫头猪:写得不错,如果再能和W3C DOM做一下对比就更好了。
还有,这个说得不太清楚准确,

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


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

返回顶部