
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.3.1.js"></script>
<script type="text/javascript">
$(function() {
var frame = $("#frame")
var win = frame[0].contentWindow
$(win).load(function() {
var dom = win.document
$(dom.body).css({margin:'0',padding:'0',border:'0',width:'100%',height:'100%'})
.html(
'<div id="layer" style="width:100%height:100%overflow:autobackground:red">' +
'<div style="width:1000pxheight:1000px">scroll' +
'</div>' +
'</div>'
)
$("#layer", dom.body).scroll(function() {
alert('invoke')
})
})
})
</script>
</head>
<body>
<iframe id="frame" frameborder="2" scrolling="no" src="about:blank" style="width:300pxheight:200px"></iframe>
</body>
</html>
你这样是不可以的,因为内部的frame可能还没有加载完成,所以你找不到file这个对象的我的做法是,外面页面创建一个隐藏的对象,添加单机事件,该单机事件为获取uploadrame内部的对象,再给这个对象绑定事件。你是写上传图片的吧,我也用iframe写的!
首先 ,建立一个iframe对象,形成原型
var IframeOnClick = {
resolution: 200,
iframes: [ ],
interval:null,
Iframe:function() {
this.element = arguments[0]
this.cb = arguments[1]
this.hasTracked =false
},
track:function(element, cb) {
this.iframes.push(newthis.Iframe(element, cb))
if (!this.interval) {
var _this =this
this.interval = setInterval(function() { _this.checkClick() },this.resolution)
}
},
checkClick:function() {
if (document.activeElement) {
var activeElement = document.activeElement
for (var iinthis.iframes) {
if (activeElement ===this.iframes[i].element) {// user is in this Iframe
if (this.iframes[i].hasTracked ==false) {
this.iframes[i].cb.apply(window, [])
this.iframes[i].hasTracked =true
}
}else {
this.iframes[i].hasTracked =false
}
}
}
}
}
最后直接调用
IframeOnClick.track(document.getElementById("iFrame"),function() { alert('a click') })
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)