导航
var oDiv = document.getElementsByTagName('div')[0];
oDiv.style.width = '200px';
oDiv.style.height = '200px';
oDiv.style.backgroundColor = 'green';
oDiv.style.border = '2px solid #eee';
oDiv.style.borderWidth = '2px';
oDiv.style.borderStyle = 'solid';
oDiv.style.position = 'absolute';
oDiv.style.left = '200px';
oDiv.style.top = '300px';
oDiv.style.cssFloat = 'left';
element.style.xxx
-> 可读可写element.style.width // '200px'
element.style.border = '5px solid #000'
element.style.borderWidth = '5px'
element.style.borderStyle = 'solid'
element.style.borderColor = '#000'
css
element.style.float = 'left'
element.style.cssFloat = 'left'
function getStyles(elem, prop) {
if (window.getComputedStyle) {
if (prop) {
return window.getComputedStyle(elem, null)[prop];
} else {
return window.getComputedStyle(elem, null);
}
} else {
if (prop) {
return window.currentStyle(elem, null)[prop];
} else {
return window.currentStyle(elem, null);
}
}
}
div::after {
content: "";
display: block;
width: 100px;
height: 100px;
background-color: pink;
}
window.getComputedStyle(div, 'after').width; // '100px'
window.getComputedStyle(div, 'after').height; // '100px'
<style type="text/css">
.box {
...
}
</style>
<body>
<div class="box"></div>
<script type="text/javascript">
const oBox = document.getElementsByClassName("box")[0];
oBox.onclick = function() {
this.style.backgroundColor = 'pink';
this.style.width = '200px';
this.style.height = '200px';
}
</script>
</body>
// 👇🏻 推荐下面方式 👇
<style type="text/css">
.box {
...
}
.box.active {
width: 200px;
height: 200px;
background-color: pink;
}
</style>
<body>
<div class="box"></div>
<script type="text/javascript">
const oBox = document.getElementsByClassName("box")[0];
oBox.onclick = function() {
this.className += ' active';
}
</script>
</body>