Appearance
DOM 属性工具
一组 DOM 属性 / 内联样式的批量写入与移除函数:setStyle / setStyles、setAttrib / setAttribs、removeAttrib / removeAttribs / removeAllAttribs。
共性:目标可以是单个 Element、CSS 选择器(取 document 下全部匹配)或它们的数组;批量版本跳过 null / undefined 值。
setStyle / setStyles
设置内联样式(setStyle 是 setStyles 的单属性版)。
ts
import { setStyle, setStyles } from '@qingpai/utils'
setStyle(element, 'width', '100px')
setStyle('.qp-pinyin', 'fontSize', '14px')
setStyles(element, { width: '100px', height: 'auto' })
setStyles([elementA, '.selector'], { display: 'none' })参数
| 名称 | 类型 | 描述 |
|---|---|---|
elm | Element | string | Array<Element | string> | 目标元素、CSS 选择器或它们的数组 |
name / styles | string / Record<string, string | null | undefined> | setStyle 传样式名(camelCase,如 fontSize)与值;setStyles 传样式表,null / undefined 值跳过 |
setAttrib / setAttribs
设置属性(setAttrib 是 setAttribs 的单属性版)。
ts
import { setAttrib, setAttribs } from '@qingpai/utils'
setAttrib(element, 'data-block-type', 'image')
setAttribs('.qp-pinyin', { 'data-tianzige': '1' })
setAttribs([elementA, '.selector'], { class: 'qp-emphasis' })参数
| 名称 | 类型 | 描述 |
|---|---|---|
elm | Element | string | Array<Element | string> | 目标元素、CSS 选择器或它们的数组 |
name / attrs | string / Record<string, string | null | undefined> | setAttrib 传属性名与值;setAttribs 传属性表,null / undefined 值跳过 |
removeAttrib / removeAttribs / removeAllAttribs
移除属性(removeAttrib 是 removeAttribs 的单属性版;removeAllAttribs 移除全部属性)。
ts
import { removeAttrib, removeAllAttribs } from '@qingpai/utils'
removeAttrib(element, 'data-block-type')
removeAttribs('.qp-pinyin', ['class', 'style'])
removeAllAttribs(element) // 含 class / style / data-* 全清,内联样式随 style 属性一并清除参数
| 名称 | 类型 | 描述 |
|---|---|---|
elm | Element | string | Array<Element | string> | 目标元素、CSS 选择器或它们的数组 |
name / names | string / string[] | 要移除的属性名(removeAllAttribs 无此参数,全清) |