Home >Web Front-end >JS Tutorial >js code to add styles to specified elements_javascript skills

js code to add styles to specified elements_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:39:52976browse

This function mainly adds styles to the specified elements. Equivalent to addClass(class) in Jquery--add the specified class name to each matching element.

Required in Jquery class (String): One or more CSS class names to be added to the element, separated by spaces. And this function also has such requirements.

Copy code The code is as follows:

div{ border:1px solid #ccc; width:200px ; height:200px;}
.a{ background:#900; }
.b{ font-size:30px; font-weight:bold;}


Copy code The code is as follows:

function addClass(elements, value)
{
if ( !elements.className) {
elements.className = value;
}
else
{
newClass = elements.className;
newClass = " ";
newClass = value ;
elements.className = newClass;
}
}
window.onload = function ()
{
var test = document.getElementById('test');
alert(test.className);
addClass(test, 'a b');
//addClass(test, 'b');
}


This is the test layer
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn