请看被打开的子窗口继承父窗口定义的styleSheets的例子

发表于:2007-06-30来源:作者:点击数: 标签:
test.htm html head titletest/title style type=text/css id=fatherStyle body {background:#efefef} .s1 {background: red} /style /head body input type=button value=open onclick=window.open(@#son.htm@#,@#@#) /body /html -------------------------
test.htm

<html>
<head>
<title>test</title>
<style type=text/css id=fatherStyle>
body {background:#efefef}
.s1 {background: red}
</style>
</head>

<body>
<input type=button value=open onclick=window.open(@#son.htm@#,@#@#)>
</body>
</html>
------------------------------------------

son.htm

<HTML>
<HEAD><TITLE></TITLE>
<style type=text/css id=sonStyle>
</style>
<script>
bgd=opener.document.styleSheets(0).rules(1).style.background;
name=opener.document.styleSheets(0).rules(1).selectorText;
document.styleSheets.sonStyle.addRule(name,"background:"+bgd);

//以上几条语句使得本页面可以使用父窗口定义的s1,但是这很麻烦,有什么简单的办法可以继承父窗口定义的所有styleSheets吗?象下面的语句就出错:
//document.styleSheets.sonStyle.rules=opener.document.styleSheets.fatherStyle.rules;

function changeStyle(){
d1.className="s1";
}   
</script>

</HEAD>
<BODY>
<span id=d1>测试文字</span><br>
<input type=button value=change onClick=changeStyle()>
</body>
</html>

原文转自:http://www.ltesting.net