Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

js获取到dom元素后如何修改dom的子元素样式?

比如

<div class="a">
    <div class="b"></div>
    <div class="c"></div>
</div>

我通过js获取到classa的dom元素了,然后我想修改classc的样式该怎么写?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
let dom = document.querySelector('.a');
// 通过 classList 修改
dom.classList.add('a-add');
dom.classList.remove('a');
// 直接赋值
dom.className = 'a-class-name';
// setAttribute
dom.setAttribute('class', 'a-set-attribute');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...