加载后的iframe javascript

加载后的iframe javascript,javascript,html,css,iframe,Javascript,Html,Css,Iframe,当iframe加载时,我使用css类“active”,当iframe停止加载时,我切换到“inactive”类 <script type="text/javascript"> function frameload(){ document.getElementById("loadnav").className = "active"; } document.getElementById("loadnav").className = "inactive"; </script&

当iframe加载时,我使用css类“active”,当iframe停止加载时,我切换到“inactive”类

<script type="text/javascript">
function frameload(){
    document.getElementById("loadnav").className = "active";
}
document.getElementById("loadnav").className = "inactive";
</script>
<iframe src="https://www.wikipedia.org/" onload="frameload()"></iframe>

函数frameload(){
document.getElementById(“loadnav”).className=“活动”;
}
document.getElementById(“loadnav”).className=“非活动”;

问题是加载后不会返回到“非活动状态”。

是否尝试使用jQuery。说明文档.getElementById(“loadnav”).className=“inactive”;在加载iframe之后执行

$('iframe').load(function() {
 document.getElementById("loadnav").className = "inactive";
});

您是否尝试使用jQuery。说明文档.getElementById(“loadnav”).className=“inactive”;在加载iframe之后执行

$('iframe').load(function() {
 document.getElementById("loadnav").className = "inactive";
});
两件事

  • iframe上没有
    id
    ,因此
    文档。getElementById
    不会做任何事情

  • 因为
    document.getElementById(“loadnav”).className=“inactive”,所以需要为
    DOMContentLoaded
    提供一些侦听器试图在
    iframe
    实际出现在页面上之前运行

  • iframe{
    宽度:100%;
    身高:100%;
    }
    iframe.inactive{
    边框:2倍纯红;
    }
    iframe.active{
    边框颜色:绿色;
    }
    
    函数frameload(){
    document.getElementById(“loadnav”).className=“活动”;
    }
    函数init(){
    document.getElementById(“loadnav”).className=“非活动”;
    document.getElementById(“loadnav”).addEventListener(“load”,frameload);
    }
    document.addEventListener('DOMContentLoaded',init);
    
    两件事

  • iframe上没有
    id
    ,因此
    文档。getElementById
    不会做任何事情

  • 因为
    document.getElementById(“loadnav”).className=“inactive”,所以需要为
    DOMContentLoaded
    提供一些侦听器试图在
    iframe
    实际出现在页面上之前运行

  • iframe{
    宽度:100%;
    身高:100%;
    }
    iframe.inactive{
    边框:2倍纯红;
    }
    iframe.active{
    边框颜色:绿色;
    }
    
    函数frameload(){
    document.getElementById(“loadnav”).className=“活动”;
    }
    函数init(){
    document.getElementById(“loadnav”).className=“非活动”;
    document.getElementById(“loadnav”).addEventListener(“load”,frameload);
    }
    document.addEventListener('DOMContentLoaded',init);
    
    我认为您必须删除初始类我认为您必须删除初始类