首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >document.body.setAttribute在IE-7中不起作用

document.body.setAttribute在IE-7中不起作用
EN

Stack Overflow用户
提问于 2011-09-17 14:30:32
回答 2查看 2.1K关注 0票数 2

我已经在我的.aspx页面中添加了"test.js“文件(在标题部分)。

在test.js中我添加了一个脚本"document.body.setAttribute("onload","testload()");“

它在IE-8-9,Mozilla,Chrome和加载testLoad()函数中都工作得很好。

但它在IE-7中不起作用。

如何在IE-7中设置test.js文件中的body属性。

EN

回答 2

Stack Overflow用户

发布于 2011-09-17 14:37:45

好呀

代码语言:javascript
复制
window.onload = testload;

代码语言:javascript
复制
window.onload = function()
{
    //Some codes
}

请注意,body.onloadwindow.onload是不同的。如果要在加载所有资源后执行事件处理程序,请使用window.onload

票数 1
EN

Stack Overflow用户

发布于 2011-12-11 02:58:18

IE7不支持obj.setAttribute('onload', doSomething);。你可以用计时器来处理IE。

代码语言:javascript
复制
var myiFrame = document.createElement("iframe");
myiFrame.setAttribute("id", "myiFrame");
myiFrame.setAttribute("src", "something.aspx");
myiFrame.setAttribute("class", "myclass");
myiFrame.setAttribute("frameBorder", "0"); //For IE
myiFrame.setAttribute("hspace", "0");
//For all:
myiFrame.setAttribute("onload", "testload();"); 
document.getElementById("myDiv").appendChild(myiFrame);
//For IE:
if (isIE = /*@cc_on!@*/false) {
    setTimeout(function () { testload() }, 500);
}

就是这样。如果你还想在加载时附加事件侦听器,IE同样需要它的修复:

代码语言:javascript
复制
function testload() {
//Add event listener for click so that
//resize myiFrame in case any changes occur in size of content when user clicks
    var content = document.getElementById("myiFrame").contentWindow.document.body;
    if (content.addEventListener) { //For all
        content.addEventListener('click', function () {
            //find the height of the internal page
            var the_height = content.scrollHeight;

            //change the height of the iframe
            document.getElementById("myiFrame").height = the_height + 10;
        }, false);
    }
    else if (content.attachEvent) { //For IE
        cerceveIci.attachEvent('onclick', function () {
            //find the height of the internal page
            var the_height = cerceveIci.scrollHeight;

            //change the height of the iframe
            document.getElementById("kk-iframe").height = the_height + 10;
        });
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7452945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档