<!DOCTYPE html>
<script>
function setit(){
if(document.all){
document.getElementById("myframe").attachEvent("onblur",dothis);
}else{
document.getElementById("myframe").contentWindow.addEventListener("blur",dothis,false);
}
}
function dothis(){
alert("blurred");
}
</script>
<body οnlοad="setit()">
<iframe width="155" height="144" id="myframe"></iframe>
<input />
(以上代码转载自: http://www.111cn.net/wy/js-ajax/72696.htm )
其中document.all为IE的属性,用来判断是否为IE浏览器。
以上代码在chrome中同样适用。
具体的添加事件格式为如下:
Mozilla中:
addEventListener的使用方式:
target.addEventListener(type, listener, useCapture);
target: 文档节点、document、window 或 XMLHttpRequest。
type: 字符串,事件名称,不含“on”,比如“click”、“mouseover”、“keydown”等。
listener :实现了 EventListener 接口或者是 JavaScript 中的函数。
useCapture :是否使用捕捉,一般用 false 。例如:document.getElementById("testText").addEventListener("keydown", function (event) { alert(event.keyCode); }, false);
IE中:
target.attachEvent(type, listener);
target: 文档节点、document、window 或 XMLHttpRequest。
type: 字符串,事件名称,含“on”,比如“onclick”、“onmouseover”、“onkeydown”等。
listener :实现了 EventListener 接口或者是 JavaScript 中的函数。 例如:document.getElementById("txt").attachEvent("onclick",function(event){alert(event.keyCode);});