菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
61
0

js中如何移除css样式?

原创
05/13 14:22
阅读数 1714

js中如何移除css样式?

dom元素应用css有两种方式:

● 通过class类名和id名应用样式

● 通过指定style属性应用样式

我们可以针对以上两种方式写移除css样式的方法

(相关课程推荐:JS视频教程

1、使用removeAttribute方法移除class、id和style属性

let app = document.getElementById('app');
app.removeAttribute('class')
app.removeAttribute('id')
app.removeAttribute('style')

2、使用setAttribute方法将class、id和style属性置空

let app = document.getElementById('app');
app.setAttribute('class', '')
app.setAttribute('id', '')
app.setAttribute('style', '')

3、使用remove移除网页中使用link标签引入的css

// es6
document.querySelectorAll('link[rel=stylesheet]').forEach(dom => dom.remove())

// es5
let links = document.querySelectorAll('link[rel=stylesheet]');
links.forEach(function (dom) {
    dom.remove()
})

发表评论

0/200
61 点赞
0 评论
收藏
为你推荐 换一批