咖啡伴侶

          呆在上海
          posts - 163, comments - 156, trackbacks - 0, articles - 2

          IE 兼容問題

          Posted on 2008-05-29 13:59 oathleo 閱讀(965) 評論(1)  編輯  收藏 所屬分類: Web

          盡管當前Web瀏覽器中W3C DOM和JavaScript的實現在不斷改進,但還是存在一些特異性和不兼容性,這使得應用DOM和JavaScript進行開發時很是頭疼。

          IE的W3C DOM和JavaScript實現最受限制。2000年初,一些統計稱IE占據了整個瀏覽器市場95%的份額,由于沒有競爭壓力,Microsoft決定不完全實現各個Web標準。

          這些特異問題大多都能得到解決,不過這樣做會讓腳本更是混亂不堪而且不合標準。例如,如果使用appendChild將<tr>元素直接增加到<table>中,則在IE中這一行并不出現,但在其他瀏覽器中卻會顯示出來。對此的解決之道是,將<tr>元素增加到表的<tbody>元素中,這種解決辦法在所有瀏覽器中都能正確工作。

          關于setAttribute方法,IE也有麻煩。IE不能使用setAttribute正確地設置class屬性。對此有一個跨瀏覽器的解決方法,即同時使用setAttribute("class", "new- ClassName") 和setAttribute("className","newClassName")。另外,

          在IE中不能使用setAttribute設置style屬性。最能保證瀏覽器兼容的技術不是<element>.setAttribute("style, "font-weight:bold;"),

          而是<element>.style.cssText = "font- weight:bold;"。


          針對 input,在 Netscape、Opera 和 Firefox 中 e.type 既可以在 appendChild 之前,也可以在其之后。但在 IE 中 type 屬性必須在前,其它屬性必須在后。

          ××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××

          應該采用:
          document.getElementById("apple") 以ID來訪問對象,且一個ID在頁面中必須是唯一的
          document.getElementsByTagName("div")[0] 以標簽名來訪問對象

          1.setAttribute(string name, string value):增加一個指定名稱和值的新屬性,或者把一個現有的屬性設定為指定的值。

          設置對象的屬性則應該采用:
          document.getElementById("apple").setAttribute("width","100")
          document.getElementsByTagName("div")[0].setAttribute("width","100")
          訪問對象的屬性則采用:
          document.getElementById("apple").getAttribute("width")
          document.getElementsByTagName("div")[0].getAttribute("width")

          我們經常需要在JavaScript中給Element動態添加各種屬性,這可以通過使用setAttribute()來實現,這就涉及到了瀏覽器的兼容性問題。
          var bar = document.getElementById("foo");
          bar.setAttribute("onclick", "javascript:alert('This is a test!');");
          這里利用setAttribute指定e的onclick屬性,簡單,很好理解。但是IE不支持,IE并不是不支持setAttribute這個函數,
          而是不支持用setAttribute設置某些屬性,例如對象屬性、集合屬性、事件屬性,也就是說用setAttribute設置style和onclick這些屬性
          在IE中是行不通的。為達到兼容各種瀏覽器的效果,可以用點符號法來設置Element的對象屬性、集合屬性和事件屬性。
          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、關于class和className
          class屬性在W3C DOM中扮演著很重要的角色,但由于瀏覽器差異性仍然存在。使用setAttribute("class", vName)語句動態設置
          Element的class屬性在firefox中是行的通的,在IE中卻不行。因為使用IE內核的瀏覽器不認識"class",要改用"className";
          同樣,firefox 也不認識"className"。所以常用的方法是二者兼備:
          ???? element.setAttribute("class", vName);
          ???? element.setAttribute("className", vName);??? //for IE

          關于IE下TABLE無法插入新行的問題
          IE下TABLE無論是用innerHTML還是appendChild插入<tr>都沒有效果,而其他瀏覽器卻顯示正常。解決他的方法是,將<tr>加到TABLE的<tbody>元素中,如下面所示:

          var row = document.createElement("tr");
          var cell = document.createElement("td");
          var cell_text = document.createTextNode("香蕉不吃蘋果");
          cell.appendChild(cell_text);
          row.appendChild(cell);
          document.getElementsByTagName("tbody")[0].appendChild(row);


          window.event

          IE:有window.event對象
          FF:沒有window.event對象。可以通過給函數的參數傳遞event對象。如onmousemove=doMouseMove(event)

          鼠標當前坐標
          IE:event.x和event.y。
          FF:event.pageX和event.pageY。
          通用:兩者都有event.clientX和event.clientY屬性。

          鼠標當前坐標(加上滾動條滾過的距離)
          IE:event.offsetX和event.offsetY。
          FF:event.layerX和event.layerY。

          標簽的x和y的坐標位置:style.posLeft 和 style.posTop
          IE:有。
          FF:沒有。
          通用:object.offsetLeft 和 object.offsetTop。

          窗體的高度和寬度
          IE:document.body.offsetWidth和document.body.offsetHeight。注意:此時頁面一定要有body標簽。
          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只有執行一個過程,而attachEvent和addEventListener執行的是一個過程列表,也就是多個過程。例如:element.attachEvent("onclick", func1);element.attachEvent("onclick", func2)這樣func1和func2都會被執行。

          標簽的自定義屬性
          IE:如果給標簽div1定義了一個屬性value,可以div1.value和div1["value"]取得該值。
          FF:不能用div1.value和div1["value"]取。
          通用:div1.getAttribute("value")。

          父節點、子節點和刪除節點
          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。

          設置CSS 的STYLE
          document.getElementById('look').style.cssText="display:none;";//通用
          document.getElementById('look').setAttribute("style","display:none;");//firefox


          Feedback

          # re: IE 兼容問題[未登錄]  回復  更多評論   

          2008-05-29 21:11 by Joshua
          是挺麻煩的。
          主站蜘蛛池模板: 崇明县| 英德市| 丹寨县| 增城市| 昌黎县| 长垣县| 景谷| 扎赉特旗| 南陵县| 临沂市| 台安县| 库伦旗| 深州市| 西乌| 龙山县| 平南县| 福州市| 新平| 资兴市| 霍林郭勒市| 将乐县| 九寨沟县| 龙井市| 兰州市| 通许县| 罗城| 涞水县| 湘乡市| 贵南县| 榆社县| 莲花县| 泽库县| 达日县| 综艺| 辽宁省| 红桥区| 北京市| 万盛区| 崇阳县| 抚顺县| 金秀|