当前位置:网站首页>JS的DOM操作——style的操作
JS的DOM操作——style的操作
2022-07-24 07:20:00 【M78_国产007】
对于JS操作文档中的元素,改变其的样式特征需要用到一个属性——style
常见操作:获取的元素点(·)style.xx(需要设置或修改的属性)
代码演示:例如修改div块的背景颜色
<style>
.box{
width: 100px;
height: 100px;
background-color: red;
}
</style>
<div class="box">
我是box盒子
</div>
<script>
var box=document.querySelector(".box")
box.style.backgroundColor="green"
</script>运行结果:

可以看到box盒子的背景色是绿色,并不是css样式设置的红色,即通过style修改了背景色。
注意:这里修改背景色,并不是修改了CSS中的background-color的属性值,而是在行内(标签内)进行了设置,因为行内设置属性的权重最大所以覆盖了css中设置的背景色。
通过调试器就可以看到:(F12快捷打开调试器)

因此在使用style时需要注意以下几点:
获取的元素点(·)style.xx(需要设置或修改的属性 ,只能操作行内样式;
属性值只能设置为字符串;
属性必须用驼峰命名法 ,例如style.background-color就是错误的,
正确写法:style.backgroundColor;
遇到与保留字一样的样式,前面应加“css”,例如float——>element.style.cssFloat。
那我们想操作css里面的属性该怎么呢?
我只能说没有办法,但是能够获取到css里面属性的值,想要操作属性还是只能用 获取的元素点(·)style.xx(需要设置或修改的属性)
那我们想要获取css里面的属性我们又应该怎么办呢?
window对象为我们提供了一个方法——getComputedStyle(当前元素节点),返回css样式对象,里面包含了我们在css里面设置的属性的属性值。
代码演示:我们获取一下这个对象,打印到控制台看一下
<style>
.box{
width: 100px;
height: 100px;
background-color: red;
}
</style>
<div class="box">
我是box盒子
</div>
<script>
var box=document.querySelector(".box")
var cssobj=window.getComputedStyle(box)
console.log(cssobj);
</script>控制台打印结果:

可以看到这个对象里面包含的属性有很多,并且能够看到我们设置的background-color的属性值,rgb(255,0,0)就是红色。
既然如此,我们就可以通过这个对象点需要的属性,就可以得到对应设置的属性值:
改进代码:
<style>
.box{
width: 100px;
height: 100px;
background-color: red;
}
</style>
<div class="box">
我是box盒子
</div>
<script>
var box=document.querySelector(".box")
var cssobj=window.getComputedStyle(box)
console.log(cssobj.width,cssobj.height,cssobj.backgroundColor);
</script>控制台打印结果:

这个window的方法,经常用于网页中模块拖动的效果的实现。
补充一点:window.getComputedStyle()不适应于IE浏览器,IE浏览器有一个它独有的方法获取css内的属性对象:element.currentStyle
边栏推荐
- fopen、fwrite、fseek、ftell、fread使用demo
- [leetcode] 11. Container with the most water - go language solution
- [leetcode] 444. Sequence reconstruction
- Variables and data types (03)
- 全国职业院校技能大赛网络安全B模块 缓冲区溢出漏洞
- Unity中使用深度和法线纹理
- Part II - C language improvement_ 4. Secondary pointer
- 解压主播狂揽4000w+播放,快手美食赛道又添新风向?
- 【C语言入门】ZZULIOJ 1011-1015
- Learning notes - distributed transaction theory
猜你喜欢

Chapter007 FPGA learning IIC bus EEPROM reading

From the perspective of CIA, common network attacks (blasting, PE, traffic attacks)

PHP escape string

Variables and data types (03)

【FreeRTOS】11 软件定时器

【LeetCode-简单】20. 有效的括号 - 栈

C language from entry to soil function

MITRE ATT&CK超详细学习笔记-01(背景,术语,案例)

Chapter007-FPGA学习之IIC总线EEPROM读取

Take you to learn C step by step (second)
随机推荐
Jenkins 详细部署
Using depth and normal textures in unity
Pytorch deep learning practice lesson 10 / assignment (basic CNN)
mysql查询当前节点的所有父级
Part I - Fundamentals of C language_ 11. Comprehensive project - greedy snake
Empty cup mentality, start again
numpy.inf
C language from entry to soil (I)
Use dichotomy to find specific values from the array
sqli-labs简单安装
Bookkeeping app: xiaoha bookkeeping 2 - production of registration page
MongoDB应用场景及选型(海量数据存储选型)
MySQL语句
Part II - C language improvement_ 3. Pointer reinforcement
Variables and data types (03)
23.组件自定义事件
[leetcode] 11. Container with the most water - go language solution
【Word】如何生成左侧的索引目录
【FreeRTOS】11 软件定时器
[PTA] group programming ladder competition - Summary of exercises L3 (incomplete)