Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在第二次加载时更改iframe样式,不';我不能在Internet Explorer 9中工作_Javascript_Jquery_Html_Css_Iframe - Fatal编程技术网

Javascript 在第二次加载时更改iframe样式,不';我不能在Internet Explorer 9中工作

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上不起作用。做这件事的

我试图在第二次加载时更改iframe的样式。我试过这个:

$(函数(){
var loadCnt=0;
$('#iframem').load(函数(){
如果(loadCnt>0){
$(#idiv”)。宽(453)。高(349);
$(“#iframem”).css(“左”,“-656px”);
$(“#iframem”).css(“顶部”,“-250px”);
}
++负载碳纳米管;
});
});
HTML:



它工作得很好,但在InternetExplorer9上不起作用。做这件事的正确方法是什么?

我认为你的方法有一个逻辑上的问题,我不确定你是如何做到的。由于JavaScript只在页面加载后运行,这意味着在代码中,您将永远无法获得大于1的
loadCnt
——因为每次加载页面时,它都会再次将其设置为0

为了实现您所描述的目标,您需要采取不同的方法,有几种方法:

  • 使用服务器端语言,并在设置 负载计数

  • 如果您的页面通过加载并注入,那么,例如,如果您的代码位于主页上的第一个“real” 将页面加载到
    loadCnt
    ,然后在 你再次“加载”主页,实际上你并没有加载整个页面 JavaScript脚本,只需将HTML重新注入到文档中,然后可以添加一个使用
    loadCnt
    的侦听器

  • 使用JavaScript localStorage-在第一次加载时,您将设置 在您将能够访问的本地存储对象上计数器为0 在以后加载时检索:

    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');
    }
    
  • 使用Javascript cookie执行与前面的逻辑相同的操作,但是在cookie中(因为localStorage可能存在兼容性问题)


  • 您应该始终使用
    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。。。。。。。。。。。。。。。。。。。