返回列表

JavaScript操作iframe(二)

默认分类 2014/09/17 01:07

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</head>
<body>
<input type="submit" value=" OK " onclick="OK()"/>

<script language="JavaScript">
function loaded() {
    alert("Local iframe is now loaded."); 
    // 操作iframe中内容的CSS
    var iframe = window.frames[0];
    var body = iframe.document.body;

    body.style.marginTop = 50;
    body.style.padding = 0;
    body.style.fontSize = 30;
    body.style.textAlign = 'center';
    body.style.backgroundColor = 'red';
    body.style.color = 'blue';
}

function OK() {
    var iframe = document.createElement("iframe"); 
    iframe.src = "about:blank"; 

    if (iframe.attachEvent){ 
        iframe.attachEvent('onload', function(){ loaded();}); 
    } 
    else { 
        iframe.onload = function(){ loaded(); }; 
    } 

    document.body.appendChild(iframe); 
}
</script>
</body>
</html>