火狐和IE的window.event對象詳解

FF的FIREBUG,不僅能測試JS還能檢查CSS錯誤,是一般常用的。
但它主要檢查FF方面的錯誤,對IE就無能爲力了。
要測試IE,就用ieTester,它可以測試IE幾乎所有版本(1.0恐怕也用不到測試了),用法也很方便。

 

至於JS對不同瀏覽器的兼容注意事項,的確很多,下面給你的也只是部分,一般建議還是採用jquery,prototype等一些已經處理好了兼容的腳本庫,更重要的是,它們簡化了很多操作,還提供了平常你很難實現的增強功能。可以去搜搜這方面的說明文章。

JS兼容瀏覽器FF/IE技巧

做BS開發就難免會用到javascript,而每個瀏覽器對javascript的支持有不同。這就需要我們程序員去兼容他們,
不然有些瀏覽器就無法運行我們的代碼。就會造來客戶的投訴,如果讓BoSS知道了,這可不太好哦。
下面是兼容IE和FF的js腳本做法和分解(部分選自網上,經本人整理),希望對大家有幫助。
.以下以 IE 代替 Internet Explorer,以 MF/FF 代替 Mozzila Firefox
//window.event
IE:有window.event對象
FF:沒有window.event對象。可以通過給函數的參數傳遞event對象。如οnmοusemοve=doMouseMove(event)
解決方法:var event = event || window.event;
example:
<script>
  function test(event) {
  var event = event || window.event;
  //do Something
  }
</script>
<input type="button" value="click" οnclick="test(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。
解決方法:
<script>
  function test(event) {
  var event = event || window.event;
  //or var event = event ? event : window.event;//這2中都可以,也可以用if else(這簡寫)
  var x = event.offsetX || event.layerX;
  var y = event.offsetY || event.layerY;
  //do Something
  }
</script>
<div οnmοusedοwn="test(event)"></div>

//event.srcElement問題
說明:IE下,event對象有srcElement屬性,但是沒有target屬性;Firefox下,even對象有target屬性,
但是沒有srcElement屬性.
解決方法:使用obj(obj = event.srcElement ? event.srcElement : event.target;)來代替IE下的event.srcElement或者
Firefox下的event.target. 請同時注意event的兼容性問題。
//event.toElement問題
問題:
IE下,even對象有srcElement屬性,但是沒有target屬性;Firefox下,even對象有target屬性,但是沒有srcElement屬

解決方法:
var target = e.relatedTarget || e.toElement;
//標籤的x和y的座標位置:style.posLeft 和 style.posTop
IE:有。
FF:沒有。
通用:object.offsetLeft 和 object.offsetTop。
//窗體的高度和寬度
IE:document.body.offsetWidth和document.body.offsetHeight。注意:此時頁面一定要有body標籤。
FF:windows.innerWidth和windows.innerHegiht,以及document.documentElement.clientWidth和document.documentElement.clientHeight。
通用:document.body.clientWidth和document.body.clientHeight。
//添加事件
IE:element.attachEvent("onclick", function);。
FF:element.addEventListener("click", function, true)。
通 用:element.οnclick=function。雖然都可以使用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")。
//document.form.item 問題
IE:現有問題:現有代碼中存在許多 document.formName.item("itemName") 這樣的語句,不能在 MF 下運行
FF/IE: document.formName.elements["elementName"]
//集合/數組類對象問題
(1)現有問題:
  現有代碼中許多集合類對象取用時使用 (),IE 能接受,MF 不能。
(2)解決方法:
  改用 [] 作爲下標運算。如:document.forms("formName") 改爲 document.forms["formName"]。
  又如:document.getElementsByName("inputName")(1) 改爲 document.getElementsByName("inputName")[1]
//HTML 對象的 id 作爲對象名的問題
(1)現有問題
  在 IE 中,HTML 對象的 ID 可以作爲 document 的下屬對象變量名直接使用。在 MF 中不能。
(2)解決方法
  用 getElementById("idName") 代替 idName 作爲對象變量使用
//用idName字符串取得對象的問題
(1)現有問題
  在IE中,利用 eval_r(idName) 可以取得 id 爲 idName 的 HTML 對象,在MF 中不能。
(2)解決方法
  用 getElementById(idName) 代替 eval_r(idName)。
//變量名與某 HTML 對象 id 相同的問題
(1)現有問題
  在 MF 中,因爲對象 id 不作爲 HTML 對象的名稱,所以可以使用與 HTML 對象 id 相同的變量名,IE 中不能。
(2)解決方法
  在聲明變量時,一律加上 var ,以避免歧義,這樣在 IE 中亦可正常運行。
  此外,最好不要取與 HTML 對象 id 相同的變量名,以減少錯誤。
//document.getElementsByName() 和 document.all[name] 的問題
現有問題:在 IE 中,getElementsByName()、document.all[name] 均不能用來取得 div 元素
(是否還有其它不能取的元素還不知道)。
//document.all
Firefox可以兼容document.all, 但會生成一條警告。可以用getElementById("*")
或者 getElementByTagName("*")來代替
不過對於document.all.length等屬性,則完全不兼容
//input.type屬性問題
說明:IE下input.type屬性爲只讀;但是Firefox下input.type屬性爲讀寫
//window.location.href問題
說明:IE或者Firefox2.0.x下,可以使用window.location或window.location.href;Firefox1.5.x下,
只能使用window.location
解決方法:使用window.location來代替window.location.href
//模態和非模態窗口問題
說明:IE下,可以通過showModalDialog和showModelessDialog打開模態和非模態窗口;Firefox下則不能
解決方法:直接使用window.open(pageURL,name,parameters)方式打開新窗口。
如果需要將子窗口中的參數傳遞迴父窗口,可以在子窗口中使用window.opener來訪問父窗口.
例如:var parWin = window.opener; parWin.document.getElementByIdx_x("Aqing").value = "Aqing";
//frame問題
以下面的frame爲例:
<frame src="xxx.html" mce_src="xxx.html" id="frameId" name="frameName" />
(1)訪問frame對象:
IE:使用window.frameId或者window.frameName來訪問這個frame對象. frameId和frameName可以同名。
FF:只能使用window.frameName來訪問這個frame對象.
另外,在IE和Firefox中都可以使用window.document.getElementByIdx_x("frameId")來訪問這個frame對象.
(2)切換frame內容:
在IE和Firefox中都可以使用window.document.getElementByIdx_x("testFrame").src = "xxx.html"或window.frameName.location = "xxx.html"來切換frame的內容.
如果需要將frame中的參數傳回父窗口(注意不是opener,而是parent frame),可以在frme中使用parent來訪問父窗口。
例如:window.parent.document.form1.filename.value="Aqing";
//body問題
Firefox的body在body標籤沒有被瀏覽器完全讀入之前就存在;而IE的body則必須在body標籤被瀏覽器完全讀入之後才存在
//事件委託方法
IE:document.body.onload = inject; //Function inject()在這之前已被實現
FF:document.body.onload = inject();
//firefox與IE的父元素(parentElement)的區別
IE:obj.parentElement
FF:obj.parentNode
解決方法: 因爲FF與IE都支持DOM,因此使用obj.parentNode是不錯選擇
//innerText在IE中能正常工作,但是innerText在FireFox中卻不行. 需用textContent
//FireFox中設置HTML標籤的style時,所有位置性和字體尺寸的值必須後跟px。這個ie也是支持的
//父節點、子節點和刪除節點
IE:parentElement、parement.children,element.romoveNode(true)。
FF:parentNode、parentNode.childNodes,node.parentNode.removeChild(node)。
//對select的options集合操作
枚舉元素除了[]外,SelectName.options.item()也是可以的, 另外SelectName.options.length, SelectName.options.add/remove都可以在兩種瀏覽器上使用。
注意在add後賦值元素,否則會失敗
動態刪除select中的所有options:
  document.getElementByIdx_x("ddlResourceType").options.length=0;
動態刪除select中的某一項option:
  document.getElementByIdx_x("ddlResourceType").options.remove(indx);
動態添加select中的項option:
  document.getElementByIdx_x("ddlResourceType").options.add(new Option(text,value));
IE FF 動態刪除通用方法:
document.getElementByIdx_x("ddlResourceType").options[indx] = null;
//捕獲事件
問題:
FF沒有setCapture()、releaseCapture()方法
解決方法:
IE:
obj.setCapture();
obj.releaseCapture();
FF:
window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
if (!window.captureEvents) {
  o.setCapture();
}else {
  window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
}
if (!window.captureEvents) {
  o.releaseCapture();
}else {
  window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
}
//禁止選取網頁內容
問題:
FF需要用CSS禁止,IE用JS禁止
解決方法:
IE: obj.onselectstart = function() {return false;}
FF: -moz-user-select:none;
//畫圖
IE:VML。
FF:SVG。
//CSS:透明
IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60)。
FF:opacity:0.6。
//CSS:圓角
IE:不支持圓角。
FF:-moz-border-radius:4px,或者-moz-border-radius-topleft:4px;-moz-border-radius-topright:4px;
-moz-border-radius-bottomleft:4px;-moz-border-radius- bottomright:4px;。
//CSS:雙線凹凸邊框
IE:border:2px outset;。
FF:-moz- border-top-colors: #d4d0c8 white;-moz-border-left-colors: #d4d0c8 white;
-moz-border-right-colors:#404040 #808080;-moz-border-bottom-colors:#404040 #808080;。

回:發現一個錯誤,是IE能使用document.all,而FF不能使用,IE和FF都能使用document.getElementsByName

主:
//document.getElementsByName() 和 document.all[name] 的問題
現有問題:在 IE 中,getElementsByName()、document.all[name] 均不能用來取得 div 元素(是否還有其它不能取的元素還不知道)。
//document.all
Firefox可以兼容document.all, 但會生成一條警告。可以用getElementById("*")或者 getElementByTagName("*")來代替,不過對於document.all.length等屬性,則完全不兼容

發佈了45 篇原創文章 · 獲贊 7 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章