<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#box1{
width: 100px;
height: 100px;
background-color: red;
}
</style>
<script type="text/javascript">
window.onload = function(){
/*
* 点击按钮以后,修改box1的大小
*/
//获取box1
var box1 = document.getElementById("box1");
//为按钮绑定单击响应函数
var btn01 = document.getElementById("btn01");
btn01.onclick = function(){
//修改box1的宽度
/*
* 通过JS修改元素的样式:
* 语法:元素.style.样式名 = 样式值
*
* 注意:如果CSS的样式名中含有-,
* 这种名称在JS中是不合法的比如background-color
* 需要将这种样式名修改为驼峰命名法,
* 去掉-,然后将-后的字母大写
*
* 我们通过style属性设置的样式都是内联样式,
* 而内联样式有较高的优先级,所以通过JS修改的样式往往会立即显示
*
* 但是如果在样式中写了!important,则此时样式会有最高的优先级,
* 即使通过JS也不能覆盖该样式,此时将会导致JS修改样式失效
* 所以尽量不要为样式添加!important
*
*
*
*/
box1.style.width = "300px";
box1.style.height = "300px";
box1.style.backgroundColor = "yellow";
};
//点击按钮2以后,读取元素的样式
var btn02 = document.getElementById("btn02");
btn02.onclick = function(){
//读取box1的样式
/*
* 语法:元素.style.样式名
*
* 通过style属性设置和读取的都是内联样式
* 无法读取样式表中的样式
*/
//alert(box1.style.height);
alert(box1.style.width);
};
};
</script>
</head>
<body>
<button id="btn01">点我一下</button>
<button id="btn02">点我一下2</button>
<br /><br />
<div id="box1"></div>
</body>
</html>
用JS中DOM操作CSS(内联样式)
原创Frank___7 博主文章分类:JAVASCRIPT学习 ©著作权
上一篇:JAVA常用的环境变量配置
下一篇:JS中遍历数组的两种方式
-
js获取节点 dom操作
接口 nodeType常量 nodeType值 备注
职场 js 休闲 dom操作 -
js DOM子节点
DOM元素节点各个方法各个方法 childNodes属性会获取包括文本节点在呢的所有节
js javascript dom css ci -
vue获取dom节点
< p ref=“text” id=“a”>单元测试< /p>< span @mouseenter=“but”>点击
vue 钩子函数 单元测试 生命周期 -
DOM创建并添加节点
operate node div{ border:#00ccff 1
DOM html css -
React获取DOM节点
1、问题背景 利用React获取DOM节点,需要利用ref属性2、实现源码 React获取DOM节点 3、实现结果 ...
html javascript react.js 知识 -
EXCEL导入java解析数字乱码
依赖的pom <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.15</version> </dependency> <dependency>
EXCEL导入java解析数字乱码 蓝桥杯 gnu postman apache