Function styleheaderssiblings () {if (!document.getelementsbytagname) return false;var Headers = document.getelementsbytagname ("H1"); for (var i = 0;i < headers.length; i++) {var elem = getnextelement (headers[i].nextsibling);//elem.style.color = "Red";//elem.style.fontsize = "1.2em";//The following uses the ClassName property, which is a substitution, not an append//elem.classname = "Intro"; //.intro{ color:red;font-size:1.2em;} AddClass (Elem, "Intro");//Call encapsulation function}}//elem.classname += " intro" note, The first character of the intro is a space, which means that the new CALSS attribute is appended to the original attribute//If there is no attribute in the original, then the classname is directly assigned, without the need to append//So we can encapsulate the above steps into a function for general function AddClass (Element,value) {if (!element.classname) {element.classname = value;} else{newclassname = element.classname;newclassname += "";newclassname += value; Element.cLassname = newclassname;}}
Aaclass () encapsulates a common function, classnames property substitution instead of appending an issue