Ai
1 Star 0 Fork 12

NextZero/dom.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
replaceClass.js 977 Bytes
一键复制 编辑 原始数据 按行查看 历史
Yaohaixiao 提交于 2023-10-12 16:04 +08:00 . chore: 调整单测代码
import isElement from './isElement'
import hasClass from './hasClass'
/**
* 替换 DOM 节点的样式类
* ========================================================================
* @method replaceClass
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/classList
* @see https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/replace
* @param {HTMLElement} el - 要替换样式的 DOM 节点
* @param {String} oldCls - 要替换的目标样式名称
* @param {String} newCls - 新的样式名称
*/
const replaceClass = (el, oldCls, newCls) => {
let allClass
let classList
if (!isElement(el)) {
return false
}
allClass = el.className
if (!allClass || !hasClass(el, oldCls)) {
return false
}
classList = el.classList
if (classList?.replace) {
classList.replace(oldCls, newCls)
} else {
el.className = allClass.replace(oldCls, newCls)
}
}
export default replaceClass
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/nextzero/dom.js.git
git@gitee.com:nextzero/dom.js.git
nextzero
dom.js
dom.js
main

搜索帮助