盡管當(dāng)前Web瀏覽器中W3C DOM和JavaScript的實(shí)現(xiàn)在不斷改進(jìn),但還是存在一些特異性和不兼容性,這使得應(yīng)用DOM和JavaScript進(jìn)行開(kāi)發(fā)時(shí)很是頭疼。
IE的W3C DOM和JavaScript實(shí)現(xiàn)最受限制。2000年初,一些統(tǒng)計(jì)稱(chēng)IE占據(jù)了整個(gè)瀏覽器市場(chǎng)95%的份額,由于沒(méi)有競(jìng)爭(zhēng)壓力,Microsoft決定不完全實(shí)現(xiàn)各個(gè)Web標(biāo)準(zhǔn)。
這些特異問(wèn)題大多都能得到解決,不過(guò)這樣做會(huì)讓腳本更是混亂不堪而且不合標(biāo)準(zhǔn)。例如,如果使用appendChild將<tr>元素直接增加到<table>中,則在IE中這一行并不出現(xiàn),但在其他瀏覽器中卻會(huì)顯示出來(lái)。對(duì)此的解決之道是,將<tr>元素增加到表的<tbody>元素中,這種解決辦法在所有瀏覽器中都能正確工作。
關(guān)于setAttribute方法,IE也有麻煩。IE不能使用setAttribute正確地設(shè)置class屬性。對(duì)此有一個(gè)跨瀏覽器的解決方法,即同時(shí)使用setAttribute("class", "new- ClassName") 和setAttribute("className","newClassName")。另外,
在IE中不能使用setAttribute設(shè)置style屬性。最能保證瀏覽器兼容的技術(shù)不是<element>.setAttribute("style, "font-weight:bold;"),
而是<element>.style.cssText = "font- weight:bold;"。
針對(duì) input,在 Netscape、Opera 和 Firefox 中 e.type 既可以在 appendChild 之前,也可以在其之后。但在 IE 中 type 屬性必須在前,其它屬性必須在后。
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
應(yīng)該采用:
document.getElementById("apple") 以ID來(lái)訪問(wèn)對(duì)象,且一個(gè)ID在頁(yè)面中必須是唯一的
document.getElementsByTagName("div")[0] 以標(biāo)簽名來(lái)訪問(wèn)對(duì)象
1.setAttribute(string name, string value):增加一個(gè)指定名稱(chēng)和值的新屬性,或者把一個(gè)現(xiàn)有的屬性設(shè)定為指定的值。
設(shè)置對(duì)象的屬性則應(yīng)該采用:
document.getElementById("apple").setAttribute("width","100")
document.getElementsByTagName("div")[0].setAttribute("width","100")
訪問(wèn)對(duì)象的屬性則采用:
document.getElementById("apple").getAttribute("width")
document.getElementsByTagName("div")[0].getAttribute("width")
我們經(jīng)常需要在JavaScript中給Element動(dòng)態(tài)添加各種屬性,這可以通過(guò)使用setAttribute()來(lái)實(shí)現(xiàn),這就涉及到了瀏覽器的兼容性問(wèn)題。
var bar = document.getElementById("foo");
bar.setAttribute("onclick", "javascript:alert('This is a test!');");
這里利用setAttribute指定e的onclick屬性,簡(jiǎn)單,很好理解。但是IE不支持,IE并不是不支持setAttribute這個(gè)函數(shù),
而是不支持用setAttribute設(shè)置某些屬性,例如對(duì)象屬性、集合屬性、事件屬性,也就是說(shuō)用setAttribute設(shè)置style和onclick這些屬性
在IE中是行不通的。為達(dá)到兼容各種瀏覽器的效果,可以用點(diǎn)符號(hào)法來(lái)設(shè)置Element的對(duì)象屬性、集合屬性和事件屬性。
document.getElementById("foo").className = "fruit";
document.getElementById("foo").style.cssText = "color: #00f;";
document.getElementById("foo").style.color = "#00f";
document.getElementById("foo").onclick= function () { alert("This is a test!"); }
2、關(guān)于class和className
class屬性在W3C DOM中扮演著很重要的角色,但由于瀏覽器差異性仍然存在。使用setAttribute("class", vName)語(yǔ)句動(dòng)態(tài)設(shè)置
Element的class屬性在firefox中是行的通的,在IE中卻不行。因?yàn)槭褂肐E內(nèi)核的瀏覽器不認(rèn)識(shí)"class",要改用"className";
同樣,firefox 也不認(rèn)識(shí)"className"。所以常用的方法是二者兼?zhèn)洌?br />???? element.setAttribute("class", vName);
???? element.setAttribute("className", vName);??? //for IE
關(guān)于IE下TABLE無(wú)法插入新行的問(wèn)題
IE下TABLE無(wú)論是用innerHTML還是appendChild插入<tr>都沒(méi)有效果,而其他瀏覽器卻顯示正常。解決他的方法是,將<tr>加到TABLE的<tbody>元素中,如下面所示:
var row = document.createElement("tr");
var cell = document.createElement("td");
var cell_text = document.createTextNode("香蕉不吃蘋(píng)果");
cell.appendChild(cell_text);
row.appendChild(cell);
document.getElementsByTagName("tbody")[0].appendChild(row);
window.event
IE:有window.event對(duì)象
FF:沒(méi)有window.event對(duì)象。可以通過(guò)給函數(shù)的參數(shù)傳遞event對(duì)象。如onmousemove=doMouseMove(event)
鼠標(biāo)當(dāng)前坐標(biāo)
IE:event.x和event.y。
FF:event.pageX和event.pageY。
通用:兩者都有event.clientX和event.clientY屬性。
鼠標(biāo)當(dāng)前坐標(biāo)(加上滾動(dòng)條滾過(guò)的距離)
IE:event.offsetX和event.offsetY。
FF:event.layerX和event.layerY。
標(biāo)簽的x和y的坐標(biāo)位置:style.posLeft 和 style.posTop
IE:有。
FF:沒(méi)有。
通用:object.offsetLeft 和 object.offsetTop。
窗體的高度和寬度
IE:document.body.offsetWidth和document.body.offsetHeight。注意:此時(shí)頁(yè)面一定要有body標(biāo)簽。
FF:window.innerWidth和window.innerHegiht,以及document.documentElement.clientWidth和document.documentElement.clientHeight。
通用:document.body.clientWidth和document.body.clientHeight。
添加事件
IE:element.attachEvent("onclick", func);。
FF:element.addEventListener("click", func, true)。
通用:element.onclick=func。雖然都可以使用onclick事件,但是onclick和上面兩種方法的效果是不一樣的,onclick只有執(zhí)行一個(gè)過(guò)程,而attachEvent和addEventListener執(zhí)行的是一個(gè)過(guò)程列表,也就是多個(gè)過(guò)程。例如:element.attachEvent("onclick", func1);element.attachEvent("onclick", func2)這樣func1和func2都會(huì)被執(zhí)行。
標(biāo)簽的自定義屬性
IE:如果給標(biāo)簽div1定義了一個(gè)屬性value,可以div1.value和div1["value"]取得該值。
FF:不能用div1.value和div1["value"]取。
通用:div1.getAttribute("value")。
父節(jié)點(diǎn)、子節(jié)點(diǎn)和刪除節(jié)點(diǎn)
IE:parentElement、parement.children,element.romoveNode(true)。
FF:parentNode、parentNode.childNodes,node.parentNode.removeChild(node)。
CSS:透明
IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60)。
FF:opacity:0.6。
設(shè)置CSS 的STYLE
document.getElementById('look').style.cssText="display:none;";//通用
document.getElementById('look').setAttribute("style","display:none;");//firefox