在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;
3. 访问元素的"class" 像"float"一样,"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");
This syntax would also apply using the setAttribute method.
4. 访问<label>标签中的"for" 就第3点中所提到的,我们同样需要使用不现的句法区分来访问<label>标签中的"for": 在IE中这样写: var myObject = document.getElementById("myLabel"); var myAttribute = myObject.getAttribute("htmlFor"); 在Firefox中这样写: var = document.getElementById("myLabel"); var myAttribute = myObject.getAttribute("for"); 5. 获取鼠标指针的位置 计算出鼠标指针的位置对你来说可能是非常少见的,不过当你需要的时候,在IE和Firefox中的句法是不同的。这里所写出的代码将是最最基本的,也可能是某个复杂事件处理中的某一个部分。但他们可以解释其中的异同点。同时,必须指出的是结果相对于Firefox,IE会有更在的不同,这种方法本身就是有BUG的。通常,这种不同可以用"拖动位置"来得到补偿,但,那是另外一个主题的文章了: ) !