您的位置:首页 > 其它

firefox、IE下的几个不同属性的方法调用

2014-07-07 00:00 302 查看
声明:document.all:该对象只有IE中才存在,故用该对象来判断浏览器的类型
一、IFRAME的对象
在IE下可通过document.frames["id"];得到该IFRAME对象,
而在火狐下则是通过document.getElementById("content_panel_if").contentWindow;
代码如下:

if(!document.all){ //火狐中得到IFRAME的对象

_Frame=document.getElementById("id").contentWindow;

}else{

_Frame=document.frames["id"];

}

二、得到对象的第一个子元素
IE的写法: _tbody=_table.childNodes[0]
在FF中,firefox会在子节点中包含空白则第一个子节点为空白"#text", 而ie不会返回空白
可以通过if("#text" != node.nodeName)过滤掉空白子对象

三、添加背景图片

if(!document.all){

var _tempSrc="xx\xx\xx.gif";

_tempSrc=_tempSrc.replace(/\\/g,"/"); //全部替换

markerDiv.style.backgroundImage="url("+_tempSrc+")";

}else{

markerDiv.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + this.icon.src

+ ", sizingmethod=scale);"; //火狐不支持这种写法

}

四、模拟CLIKC事件
如该实例:

<a href="#" onclick="test(1)" id="a3">hello</a>

<a href="#" onclick="test2(1)" id="b3">hello2</a>

<script language="javascript">

<!--

function test(num){

window.alert(num);

}

function test2(num)

{

if(document.all){ //ie下

document.getElementById("a3").click();

}

else{

var evt = document.createEvent("MouseEvents");

evt.initEvent("click", true, true);

document.getElementById("a3").dispatchEvent(evt);

}

}

//-->

</script>

event.initEvent(eventType,canBubble,cancelable) :
initEvent 该方法将初始化 Document.createEvent() 方法创建的合成 Event 对象的 type
属性、bubbles 属性和 cancelable 属性。 只有在新创建的 Event 对象被 Document 对象或 Element
对象的 dispatchEvent() 方法分派之前,才能调用 Event.initEvent() 方法。

五、attachEvent函数
if (isIE){window.attachEvent("onload", init);}else{window.addEventListener("load", init, false);}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: