The frequently used mini-tool functions are rewritten every time they are used, which is a waste of physical strength. So I decided to sort them out in stages and copy them for use ~
For the classname of a DOM node, common operations include adding, deleting, and querying. (You can add or delete a DOM node in advance .)CodeRelatively simple, directly add source code + Interface comments
/*** @ Description adds a class to the node * @ Param {domobject} node Dom node * @ Param {string} classname name of the class to be added */function addclass (node, classname) {var addclassname = trim (classname); var oriclassname = node. classname; If (oriclassname. indexof (addclassname) ===- 1) {node. classname = oriclassname + ''+ classname ;}} /*** @ description deletes a class of a node * @ Param {domobject} node Dom node * @ Param {string} classname name of the class to be deleted */Function Removeclass (node, classname) {var Reg = new Regexp ('\ B' + trim (classname) + '\ B', 'G'); node. classname = node. classname. replace (Reg ,'');} /*** @ description determines whether a node has a class * @ Param {domobject} node Dom node * @ Param {string} the class name queried by classname * @ return {Boolean} true- yes, false-No */function hasclass (node, classname) {return node. classname. indexof (TRIM (classname ))! =-1 ;} /*** @ Description: deletes the space at the beginning and end of a string * @ Param {string} STR original string * @ return {string} deletes the string after the first space * @ example trim ('Hello world ') === 'Hello world' */function trim (STR) {return Str. replace (/^ \ s + /,''). replace (/\ s + $ /,'');}
if multiple classes are added at the same time and multiple classes are deleted at the same time, the implementation principle is similar and will not be repeated.