梅雪香 2007-05-15 04:27:13 代码就不用看了,散分贴!嘿嘿
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>CommonDiv</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
<!--
//CommonDiv Object
function CommonDiv(parentObject){
this.unique = ((new Date()).valueOf()) % 604800000;
this.cDiv = null;
this.cIfrm = null;
this.cIfrmDoc = null;
this.init(parentObject);
}
CommonDiv.prototype.init = function (parentObject){
this.cDiv = document.createElement("DIV");
this.cIfrm = document.createElement("IFRAME");
this.cIfrm.frameBorder = "no";
this.cDiv.appendChild(this.cIfrm);
try{
parentObject.appendChild(this.cDiv);
}catch(e){
throw new Error("The parentObject is invalid!");
}
this.cIfrmDoc = this.cIfrm.contentWindow.document;
this.cIfrmDoc.write('<html><head></head><body></body></html>');
this.cIfrmDoc.close();
}
//set the style of div
CommonDiv.prototype.setDivStyle = function (cssText){
this.cDiv.setAttribute("id","comdiv" + this.unique);
var ss = new StyleSheet(document);
ss.insertRule("#comdiv"+ this.unique,cssText);
}
//set the style of iframe
CommonDiv.prototype.setFrameStyle = function (cssText){
this.cIfrm.setAttribute("id","comfrm" + this.unique);
var ss = new StyleSheet(document);
ss.insertRule("#comfrm"+ this.unique,cssText);
}
//set the style of frame.documentElement
CommonDiv.prototype.setStyle = function (str){
var ss = new StyleSheet(this.cIfrmDoc);
//set the style of iframe.document here
//ss.insertRule("body","background-color:#3399FF",0);
}
//StyleSheet Object
function StyleSheet(doc){
this.sso = null;
this.createStyleSheet(doc);
}
StyleSheet.prototype.createStyleSheet = function (doc){
//IE
if(doc.createStyleSheet)
this.sso = doc.createStyleSheet();
else{//FF
var styles = doc.getElementsByTagName("HEAD")[0].getElementsByTagName("style");
if(styles.length > 0)
this.sso = styles[styles.length-1];
else{
try{
this.sso=doc.createElement("STYLE");
this.sso.type="text/css";
doc.getElementsByTagName("HEAD")[0].appendChild(this.sso);
}catch(e){
this.sso = null;
}
}
}
}
StyleSheet.prototype.insertRule = function (selector,cssText){
if(this.sso.addRule)
this.sso.addRule(selector,cssText,this.sso.rules.length);
else if(this.sso.sheet)
this.sso.sheet.insertRule(selector + "{" + cssText + "}",this.sso.sheet.length);
}
//-->
</script>
</head>
<body>
<script type="text/javascript">
<!--
var cd = new CommonDiv(document.body);
cd.setDivStyle("position:absolute;");
cd.setDivStyle("border:1px solid #FF6600; width:150px; left:100px; ");
cd.setDivStyle("height:220px; top:100px;");
cd.setFrameStyle("width:100%; height:100%; border:0px; ");
var ss = document.getElementsByTagName("head")[0].getElementsByTagName("style");
//prompt("",document.getElementsByTagName("head")[0].innerHTML.replace(/\n/g,""));
//-->
</script>
</body>
</html>