javascript - 神奇的iframe现象:居然能够自动触发onclick 事件,这是什么原因??
本文介绍了javascript - 神奇的iframe现象:居然能够自动触发onclick 事件,这是什么原因??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<button id='chg'>change</button>
<iframe id='frame' src=''></iframe>
<script>
function $id(id){
return document.getElementById(id);
}
var btn=document.getElementById('chg');
var tip=true;
btn.onclick=function(){
alert('你居然在按钮没有被按下的时候就触发了!!!');
}
(function(){
var iList=document.getElementsByTagName('iframe');
for (var i=0;i<iList.length;++i)
{
iList[i].onload=function(){
this.style.height=this.contentWindow.document.documentElement.scrollHeight+'px';
}
}
}());
</script>
</body>
</html>
解决方案
兄弟这样看你看到了什么?
btn.onclick=function(){
alert('你居然在按钮没有被按下的时候就触发了!!!');
}(calegeca);
var calegeca = function() {
var iList = document.getElementsByTagName('iframe');
for (var i = 0; i < iList.length; ++i) {
iList[i].onload = function() {
this.style.height = this.contentWindow.document.documentElement.scrollHeight + 'px';
}
}
}();
尽量使用;号哦;在闭包前加,效果更好哦;
;(function(){
//calegeca
}())
这篇关于javascript - 神奇的iframe现象:居然能够自动触发onclick 事件,这是什么原因??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文