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
326 views
in Technique[技术] by (71.8m points)

html - Add a class to a DIV with javascript

Example HTML:

<div id="foo" class="class_one"></div>

How can I add the class class_two without replacing class_one?

end result:

<div id="foo" class="class_one class_two"></div>
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

See deekshith's answer below if you only need to support new browsers

Standard javascript:

document.getElementById('foo').className += ' class_two'

or JQuery:

$('#foo').addClass('class_two');

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

...