开发跨浏览器的JavaScript 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 这个跟 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); ----------------------- 1.向表中追加行: document.createElement 和document.appendChild方法可以很容易的做到向表中追加行或从头创建包含表行的新表:使用 document.createElement创建表格,在使用document.appendChild方法将这些表单元格增加到表行;接下来使用 document.appendChild将表行增加到表中。 IE允许讲tr元素增加到tbody中,而不是直接增加到table中。 <table id="myTable"> <tbody id="myTableBody"></tbody> </table> 向这个表中增加行的正确做法是把行增加到表体,而不是增加到表,如是所示: var cell=document.createElement("td").appendChild(document.createTextNode("foo"); var row = document.createElement("tr").appendChild(cell); document.getElementById("mysqlTableBody").appendChild(row); 幸运的是,这种方法在所有当前浏览器都通用,也包括IE。如果你养成习惯,总是使用表中的表体,就不用担心这个问题了。 2 通过Javascrīpt设置元素的样式 可以通过Javascrīpt使用元素的setAttribute方法设置元素的样式。例如,要把span 元素中的文本修改为采用红色粗体显示,可以使用setAttribute方法如下: var spanElement = document.getElementById("mySpan"); spanElement.setAttribute("style","font-weight:bold ; color: red;"); 除了IE,这种方法在当前其它浏览器上都是行得通的.对于IE,解决方法是使用元素 style对象的cssText属性来设置所需样式,尽管这个属性不是标准的,但是得到广泛支持, 如下所示: var spanElement = document.getElementById("mySpan"); spanElement.style.cssText = "font-weight:blod ; color:red;"; 这种方法在IE和大多数其他浏览器上都能很好好工作,只有Opera除外。为了让代码在 所有当前浏览器上都可移植,可以同时使用这两种方法,也就是既使用setAttribute方法, 又使用style对像的cssText属性,如下所示: var spanElement = document.getElementById("mySpan"); spanElement.setAttribute("style","font-weight:bold ; color: red;"); spanElement.style.cssText = "font-weight:blod ; color:red;";
推荐阅读
js模拟弹出效果代码修正版
模拟弹出效果代码修正版 从网上看到的代码,当文章内容较多时,总会提示复制代码 代码如下:--------------------------- Windows Internet Explorer --------------------------- Internet Explorer 无法打开 Inte>>>详细阅读
本文标题:开发跨浏览器的JavaScript方法说明第1/2页
地址:http://www.17bianji.com/kaifa2/JS/29503.html
1/2 1