监听iframe load事件与修改 iframe body样式

$(document).ready(function() {

CreateIframe();//创建iframe

var doc = document.getElementById('ContentFrame').contentDocument.document;

});

function changeStyle()//修改动态创建的iframe的一些样式

{

var x=document.getElementById("ContentFrame");

var y=(x.contentWindow || x.contentDocument);

if (y.document)y=y.document;

y.body.style.backgroundColor = "#f9f9f9";

y.body.style.padding = "0px";

y.body.style.margin = "10px";

y.body.style.fontSize = "small";

y.body.style.maxWidth = "100%";

}

///创建iframe 的方法

function CreateIframe() {

var iframe = document.createElement("iframe");

iframe.src = "Content.aspx?id="+$("#txid").val();//根据相应的id 在页面中显示需要的内容

iframe.setAttribute("id", "ContentFrame");

iframe.setAttribute("width", "100%");

iframe.setAttribute("height", "100%");

iframe.setAttribute("scrolling", "auto");

iframe.setAttribute("frameborder", "0");

if (iframe.attachEvent) {

iframe.attachEvent("onload", function() {

changeStyle();

});

} else {

iframe.onload = function() {

//                        alert("Local iframe is now loaded.");

changeStyle();

};

}

两段script都是百度后修改成自己要的

你可能感兴趣的:(监听iframe load事件与修改 iframe body样式)