---
title: "动态样式"
author: "sric08"
date: "2011-04-18T15:11:49Z"
tags: [".net", "CSS", "HTML", "JavaScript", "Microsoft"]
categories: ["Web前端"]
slug: "untitled"
draft: false
lastmod: "2011-04-18T15:11:49Z"
id: 1006394
url: "https://sric08.iteye.com/blog/1006394"
---

这是某种实现方式

 

```
<html>
	<head>
		<title></title>
		<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
		<style type="text/css" id="a">
.hello {
	background-color: pink;
}
</style>
		<style type="text/css">
.world {
	color: #33FF99
}
</style>
		<script language="javascript">
  var turnback=false;
  function setCSS()
  {
  	var ttt = document.getElementById("ttt");
  	if(turnback){
		a.styleSheet.cssText=".hello{background-color:pink;width:"+ttt.value+"px;height:"+ttt.value+"px}";
	}else{
		a.styleSheet.cssText=".hello{background-color:gray;width:"+ttt.value+"px;height:"+ttt.value+"px}";
	} turnback=!turnback;
  }
  </script>
	</head>
	<body>
		<input type="text" id="ttt">
		<button onclick="setCSS()" type="button">
			切换背景颜色
		</button>
		<div class="hello world">
			hello world
		</div>
		<div class="hello world">
			hello world
		</div>
		<div class="hello world">
			hello world
		</div>
		<div class="hello world">
			hello world
		</div>
	</body>
</html>
```