Native JS implementation of DOM operation notes (draft collation)

Source: Internet
Author: User

Native JS implements a series of Dom operation references:

    • Native JavaScript wrapper Dom Library
    • Siblings: Native js-the implementation of-siblings method for finding adjacent elements
    • Addclass,removeclass,hasclass,toggleclass: Native JS Add delete class
    • Native JS Add Delete class

The code is as follows:

varDom = {        /** Function Description: Whether the matching element contains the specified class * @param el specified DOM element * @param className matches the class name **/Hasclass:function(el,classname) {returnEl.className.match (NewRegExp (' (\\s|^) ' + ClassName + ' (\\s|$) ')); },        /** Function Description: Adds class * @param el specified DOM element to the specified DOM element * @param className Added class name **/AddClass:function(el,classname) {if(! This. Hasclass (El,classname)) {El.classname+= " " +ClassName; }        returnel; },        /** Function Description: Remove class * @param el specified DOM element for specified DOM element * @param className removed class name **/Removeclass:function(el,classname) {if( This. Hasclass (El,classname)) {            varReg =NewRegExp (' (\\s|^) ' + ClassName + ' (\\s|$) '); El.classname= El.className.replace (Reg, ")        }        returnel; },        /** Function Description: Adds or removes the class * @param el specified DOM element for the specified DOM element * @parm className Add or remove class name **/Toggleclass:function(el,classname) {if( This. Hasclass (El,classname)) {             This. Removeclass (El,classname); }Else{             This. addclass (El,classname); }        returnel; },        /** Function Description: Gets the current element of the sibling node * @param el current DOM element **/Siblings:function(EL) {varmatched = [];//Storing sibling nodes        varn = (El.parentnode | |{}). firstchild;  for(; n; n =n.nextsibling) {            if(N.nodetype = = = 1 && n!==el)            {Matched.push (n); }        }        returnmatched; }};

Native JS implementation of DOM operation notes (draft collation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.