獲取HTML元素css
IE:支持el.name 、el.getAttribute(name)
FF、Chrome:基本屬性支持el.name其他屬性僅支持el.getAttribute(name)
自定義屬性問題
IE下,可使用獲取常規屬性的方法來獲取自定義屬性,也可使用 getAttribute() 獲取自定義屬性;Firefox下,只能使用 getAttribute() 獲取自定義屬性。
Ajax請求
IE: new ActiveXObject()
FF、Chrome:new XMLHttpRequest()
獲取HTML元素
IE:支持el.name 、el.getAttribute(name)
FF、Chrome:基本屬性支持el.name其他屬性僅支持el.getAttribute(name)
innerText的使用
FF不支持innerText,它支持textContent來實現innerText,不過textContent沒有像innerText同樣考慮元素的display方式,因此不徹底與IE兼容。若是不用textContent,字符串裏面不包含HTML代碼也能夠用innerHTML代替。
if(document.all){
document.getElementById('element').innerText = "mytext";
} else{
document.getElementById('element').textContent = "mytext";
}
獲取可見區域、窗口的大小
有時,咱們會須要找到瀏覽器的可視位置的大小,一般咱們稱之爲"可見區域"。
在IE中這樣寫:
var myBrowserSize = [0, 0];
myBrowserSize[0] = document.documentElement.clientWidth;
myBrowserSize[1] = document.documentElement.clientHeight;
在Firefox中這樣寫:
var myBrowserSize = [0, 0];
myBrowserSize[0] = window.innerWidth; 瀏覽器
myBrowserSize[1] = window.innerHeight; 函數
Alpha 透明
這並非一個JavaScript句法問題,而是源自於CSS的Alpha透明。但當某個物體須要淡入/出則須要JavaScript來表現,這是經過訪問CSS的Alpha透明設置來完成的,一般在一個循環中完成。你須要經過來修改的JavaScript的代碼以下::
在IE中這樣寫:
#myElement { filter: alpha(opacity=50); }
在Firefox中這樣寫:
#myElement { opacity: 0.5; }
在IE中這樣寫:
var myObject = document.getElementById("myElement");
myObject.style.filter = "alpha(opacity=80)";
在Firefox中這樣寫:
var myObject = document.getElementById("myElement"); myObject.style.opacity = "0.5";
CSS "float" 值
訪問一個給定CSS 值的最基本句法是:object.style.property,使用駝峯寫法來替換有鏈接符的值,例如,訪問某個ID爲"header"的<div>的 background-color值,咱們使用以下句法:
document.getElementById("header").style.backgroundColor= "#ccc";
但因爲"float"這個詞是一個JavaScript保留字,所以咱們不能用object.style.float來訪問,這裏,咱們能夠在兩種瀏覽器中這麼作:
在IE中這樣寫:
document.getElementById("header").style.styleFloat = "left";
在Firefox中這樣寫:
document.getElementById("header").style.cssFloat = "left";
元素的推算樣式
JavaScript可使用object.style.property句法,方便地在外部訪問和修改某個CSS樣式,但其限制是這些句法只能取出已設的行內樣式或者直接由JavaScript設定的樣式。並不能訪問某個外部的樣式表。爲了訪問元素的"推算"樣式,咱們可使用下面的代碼:
在IE中這樣寫:
var myObject = document.getElementById("header");
var myStyle = myObject.currentStyle.backgroundColor;
在Firefox中這樣寫:
var myObject = document.getElementById("header");
var myComputedStyle = document.defaultView.getComputedStyle(myObject, null);
var myStyle = myComputedStyle.backgroundColor;
訪問元素的"class"
"class"是JavaScript的一個保留字,在這兩個瀏覽器中咱們使用以下句法來訪問"class"。
在IE中這樣寫:
var myObject = document.getElementById("header");
var myAttribute = myObject.getAttribute("className");
在Firefox中這樣寫:
var myObject = document.getElementById("header");
var myAttribute = myObject.getAttribute("class"); 指針
2、事件API對象
一、表示發生事件:事件
(1)非IE瀏覽器下,事件對應的函數有一個隱藏的變量e,表示發生事件。ip
(2)IE下,不須要e變量,window.event表示發生事件。ci
解決方案:用e||window.event來兼容。element
二、觸發事件對象(觸發事件的元素被認爲是目標target):字符串
(1)IE下,window.event對象有srcElement屬性,但沒有target屬性。
(2)Firefox下,e對象有target屬性,但沒有srcElement屬性。
(3)Chrome下,e對象同時具備target和srcElement屬性。
解決方案:event.srcElement ? event.srcElement : event.target來兼容。
三、按鍵碼(字符代碼):
(1)IE下,window.event對象只有keyCode屬性。
(2)FireFox下,e對象有which和charCode屬性。
(3)Opera下,e對象有keyCode和which屬性。
(4)Chrome下,e對象有keyCode、which和charCode屬性。
解決方案:用e.keyCode || e.which || e.charCode來兼容。
四、阻止事件的默認行爲:
(1)IE 中阻止事件的默認行爲須要將window.event.returnValue屬性設置爲false。
(2)非IE阻止事件的默認行爲須要調用 e.preventDefault() 方法。
解決方案:條件判斷瀏覽器是否具備event.preventDefault再作相應處理。
五、阻止事件冒泡:
(1)IE阻止事件冒泡須要設置window.event.cancelBubble = true。
(2)非IE阻止事件冒泡須要調用e.stopPropagation()。
解決方案:條件判斷瀏覽器是否具備event.stopPropagation再作相應處理。
6.獲取鼠標指針的位置 計算出鼠標指針的位置對你來講多是很是少見的,不過當你須要的時候,在IE和Firefox中的句法是不一樣的。這裏所寫出的代碼將是最最基本的,也多是某個復瑣事件處理中的某一個部分。但他們能夠解釋其中的異同點。同時,必須指出的是結果相對於Firefox,IE會有更在的不一樣,這種方法自己就是有BUG的。 在IE中這樣寫: var myCursorPosition = [0, 0]; myCursorPosition[0] = event.clientX; myCursorPosition[1] = event.clientY; 在Firefox中這樣寫: var myCursorPosition = [0, 0]; myCursorPosition[0] = event.pageX; myCursorPosition[1] = event.pageY;