js csstext方法,批量添加样式

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>

	<body>
		<div class="bgg">2333</div>
	</body>
	<script type="text/javascript">
		var bgg = document.querySelector('.bgg');
		var number=100;
		/*第一种直接通过cssText*/
		bgg.style.cssText='100px;height:200px;background:red';
		/*第二种构造函数*/
		/*//构造函数
		function createStyle(elem, props, value) {
			if(typeof props === 'object') {
				for(var key in props) {
					elem.style[key] = props[key];
				}
			}else{
				elem.style[props]=value;
			}
		};
		//调用函数
		createStyle(bgg,{
			 '100px',
			height: '100px',
			background: 'red'
		});*/
	</script>

</html>

  

原文地址:https://www.cnblogs.com/LWJ-booke/p/7728872.html