Javascript 在第二次加载时更改iframe样式,不';我不能在Internet Explorer 9中工作
我试图在第二次加载时更改iframe的样式。我试过这个:Javascript 在第二次加载时更改iframe样式,不';我不能在Internet Explorer 9中工作,javascript,jquery,html,css,iframe,Javascript,Jquery,Html,Css,Iframe,我试图在第二次加载时更改iframe的样式。我试过这个: $(函数(){ var loadCnt=0; $('#iframem').load(函数(){ 如果(loadCnt>0){ $(#idiv”)。宽(453)。高(349); $(“#iframem”).css(“左”,“-656px”); $(“#iframem”).css(“顶部”,“-250px”); } ++负载碳纳米管; }); }); HTML: 它工作得很好,但在InternetExplorer9上不起作用。做这件事的
$(函数(){
var loadCnt=0;
$('#iframem').load(函数(){
如果(loadCnt>0){
$(#idiv”)。宽(453)。高(349);
$(“#iframem”).css(“左”,“-656px”);
$(“#iframem”).css(“顶部”,“-250px”);
}
++负载碳纳米管;
});
});
HTML:
它工作得很好,但在InternetExplorer9上不起作用。做这件事的正确方法是什么?我认为你的方法有一个逻辑上的问题,我不确定你是如何做到的。由于JavaScript只在页面加载后运行,这意味着在代码中,您将永远无法获得大于1的
loadCnt
——因为每次加载页面时,它都会再次将其设置为0
为了实现您所描述的目标,您需要采取不同的方法,有几种方法:
loadCnt
,然后在
你再次“加载”主页,实际上你并没有加载整个页面
JavaScript脚本,只需将HTML重新注入到文档中,然后可以添加一个使用loadCnt
的侦听器if (localStorage.getItem("counter") === null) {
// Check if the item exists; if not set it to 0 as it is the first run
localStorage.setItem('counter', 0);
}
else {
// Get the counter - here you can do your work for when the page is loaded after the first load
localStorage.getItem('counter');
}
您应该始终使用
onload
属性来设置任何函数。在Internet Explorer中,不能按代码设置onload
事件。有办法做到这一点,我在回答的选项2中提到过
选项1
HTML
<div style="overflow: hidden; width: 377px; height: 230px; position: relative;" id="idiv">
<iframe id="iframem" src="http://www.example.com" style="border: 0pt none ; left: -1px; top: -8px; position: absolute; width: 1680px; height: 867px;" scrolling="no" onload="myIframeLoadFunction()">
</iframe>
</div>
选项2
在元素在HTML上呈现之后,需要专门绑定onload事件
JavaScript代码
var loadCnt = 0;
function myIframeLoadFunction(){
if (loadCnt >0) {
$("#idiv").width(453).height(349);
$("#iframem").css("left", "-656px");
$("#iframem").css("top", "-250px");
}
++loadCnt;
}
(function (selector) {
var loadCnt = 0, frame = $(selector).get(0);
if (frame) {
frame.onload = function () {
if (loadCnt >0) {
$("#idiv").width(453).height(349);
$("#iframem").css("left", "-656px");
$("#iframem").css("top", "-250px");
}
++loadCnt;
};
}
})('#iframem');
IE的哪个版本?IE 9。。。。。。。。。。。。。。。。。。。