JS DOM 编程复习笔记 -- 操作style样式、class、getComputedStyle(十一)

本文详细介绍了如何使用JavaScriptDOM进行样式修改,包括设置内联样式、获取元素真实样式、管理CSS类以及获取元素宽高。通过element.style属性、window.getComputedStyle()方法、element.className和element.classList属性,实现对元素样式的动态控制和查询。同时,文章提供了实用示例和代码片段,帮助读者深入理解这些操作。
摘要由CSDN通过智能技术生成

今天我们来复习如何使用JavaScript DOM来进行内联样式修改,CSS class的修改,以及获取元素的真实样式,获取元素的真实宽高等。

目录

  • element.style
  • window.getComputedStyle()
  • element.className
  • element.classList

设置内联样式

我们使用元素 style 属性来操作元素的内联样式。

style属性返回CSS属性的CSSStyleDeclaration只读对象,例如要将元素的color设置为红色

element.style.color = 'red';

如果是包含-的CSS属性,比如 -webkit-text-stroke,我们可以使用[]来访问该属性

element.style.['-webkit-text-stock'] = 'unset';

下面的表格中展示了常用的CSS属性

CSS JavaScript
background background
background-attachment backgroundAttachment
background-color backgroundColor
background-image backgroundImage
background-position backgroundPosition
background-repeat backgroundRepeat
border border
border-bottom borderBottom
border-bottom-color borderBottomColor
border-bottom-style borderBottomStyle
border-bottom-width borderBottomWidth
border-color borderColor
border-left borderLeft
border-left-color borderLeftColor
border-left-style borderLeftStyle
border-left-width borderLeftWidth
border-right borderRight
border-right-color borderRightColor
border-right-style borderRightStyle
border-right-width borderRightWidth
border-style borderStyle
border-top borderTop
border-top-color borderTopColor
border-top-style borderTopStyle
border-top-width borderTopWidth
border-width borderWidth
clear clear
clip clip
color color
cursor cursor
display display
filter filter
float cssFloat
font font
font-family fontFamily
font-size fontSize
font-variant fontVariant
font-weight fontWeight
height height
left left
letter-spacing letterSpacing
line-height lineHeight
list-style listStyle
list-style-image listStyleImage
list-style-position listStylePosition
list-style-type listStyleType
margin margin
margin-bottom marginBottom
margin-left marginLeft
margin-right marginRight
margin-top marginTop
overflow overflow
padding padding
padding-bottom paddingBottom
padding-left paddingLeft
padding-right paddingRight
padding-top paddingTop
page-break-after pageBreakAfter
page-break-before pageBreakBefore
position position
stroke-dasharray strokeDasharray
stroke-dashoffset strokeDashoffset
stroke-width strokeWidth
text-align textAlign
text-decoration textDecoration
text-indent textIndent
text-transform textTransform
top top
vertical-align verticalAlign
visibility visibility
width width
z-index zIndex

如果需要批量覆盖现有的内联样式,可以使用 cssText 属性

element.style.cssText = 'color:red; background-color:yellow';

或者也可以使用setAttribute()方法

element.setAttribute('style','color:red; background-color:yellow');

设置完成后,也可以单条修改CSS样式

element.style.color = 'blue';

如果不想覆盖掉现有的样式,也可以将新的CSS追加到原有样式的后面

element.style.cssText += 'color:red; background-color:yellow';

我们也可以封装一个公共函数,通过传入一个key-value的对象来给元素设置CSS样式

function css(e, styles) {
   
  for (const property in styles)
    e.style[property] = styles[property];
}

使用css()来设置样式

let content = document.querySelector('#content');
css(content, {
    background: 'yellow
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值