1、CSS → DOM
![](https://img.haomeiwen.com/i316258/83edb5ed9923baac.png)
![](https://img.haomeiwen.com/i316258/4f86ba3d65e48ee6.png)
![](https://img.haomeiwen.com/i316258/270c905c7888d2bc.png)
![](https://img.haomeiwen.com/i316258/b0ae8b7a2e294080.png)
2、样式更新
![](https://img.haomeiwen.com/i316258/7f917fbdc2bbacd4.png)
![](https://img.haomeiwen.com/i316258/49fd6effc37e520c.png)
⑴element.style.borderColor = 'red'; element.style.color = 'red';
问题:①更新一个属性需要一条语句②不是我们熟悉的CSS
⑵style.cssText
element.style.cssText = 'border-color:red;color:red;';
以上两种【样式混在逻辑中】!
⑶更新class 【推荐】
//CSS
.invalid{border-color:red;
color:red;
}
element.className += ' invalid';
【问题:】一次更新很多元素的样式 ??
更换样式表。
3、获取样式
![](https://img.haomeiwen.com/i316258/45eb43404cf8e5e8.png)
![](https://img.haomeiwen.com/i316258/a27d565218d8a4e0.png)
4、总结
![](https://img.haomeiwen.com/i316258/e342cc0a60cf9883.png)
更新样式
Q1:addEventListener($('update')...)中addEventListener??$('update')??
Q2:border-radius ??
Q3:box-shadow: 2px 2px 3px #ededed inset; ??inset??
Q4:JS什么意思???
<!DOCTYPE html>
<html>
<head>
<title>className - 更新样式</title>
<style>
button{padding: 4px 5px;margin-left: 10px;}
.u-txt{width: 215px;padding: 4px 5px;border: 1px solid #ababab;border-radius: 3px;box-shadow: 2px 2px 3px #ededed inset;font-size: 14px;font-weight: bold;}
.invalid{border-color: red;color: red;}
</style>
</head>
<body>
<input id="mobile" class="u-txt" value="13564782365">
<button id="update">更新样式</button>
<script src="util.js"></script>
<script>
Util.addEventListener($('update'), 'click', updateStyle);
function updateStyle(){
var element = $('mobile');
element.className += ' invalid';
}
</script>
</body>
</html>```
//JS
function $(id){
return typeof id === "string" ? document.getElementById(id) : id;
}
var Util = (function(document, util){
util = util || {};
util.addEventListener = function(element, type, listener){
if(element.addEventListener){
element.addEventListener(type,listener,false);
} else {
element.attachEvent('on' + type, listener);
}
}
return util;
})(document,Util);```
网友评论