js批量设置样式的三种方法不推荐使用with

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

js批量设置样式的三种方法不推荐使用with

  2020-05-13 我要评论
一般我们都用css来批量设置样式,现在我们用js也可以批量设置样式:
总结三种方法如下
复制代码 代码如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style type="text/css">
#div1{ width:100px; height:100px; background:#069;}
</style>
<script type="text/javascript">
//第一种使用JSON
function setStyle(obj,json){
for(var i in json)
{
obj.style[i]=json[i];
}
}
window.onload=function(){
var oDiv=document.getElementById('div1');
// setStyle(oDiv, {width: '200px', background: 'red'}); //第一种
// oDiv.style.cssText="width: 200px; height:300px; background:yellow;"; //第二种 使用cssText
  //使用第三种 with (不推荐使用)
with(oDiv.style)
{
width='300px';
height='500px';
background='yellow';
}
};
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们