Native JS implements several common DOM manipulation APIs

Source: Internet
Author: User

Native implementation of jquery sibling method
<Body><Span> I'm the span tag</Span><Div> I was a div</Div><H1Id="H1" > I am the title</H1><P> I was a paragraph</P><ScriptType="Text/javascript" >Gets the sibling node of the elementfunctionSiblings(o) {Parameter o is the brother node that you want to take, and you pass that element in.var a = [];Defines an array that is used to store O's sibling elementsPreviousSibling returns the previous element at the same node tree levelvar p = o.previoussibling;while (p) {First Take o the front of the sibling element to determine whether there is a sibling element, if there is a down execution, p represents previoussiblingif (P.nodetype = = =1) {A.push (P);} p = p.previoussibling//finally assigns the previous node to P}a.reverse (); //reverse the order, so that the order of the elements is sequential //nextsibling returns the next node at the same node tree level Span class= "Hljs-keyword" >var n = o.nextsibling; //then fetch o The following sibling element while (n) {// Judging if there is no next sibling knot, n is nextsibling meaning if (n.nodetype = = 1) {//determines whether the element node is A.push (n);} n = n.nextsibling;} return a//finally return this group of elements in the order from eldest to youngest} var OH = document.getElementById ( "H1"); Console.log (Siblings ( OH)); //[span, Div, p, script]</script ></BODY>       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
Native implementation of JQuery's class selector
Simulating jquery's class selector with an arrayfunction getclassname (ParentId, Newclassname) {var Allclasselem = Parentid.getelementsbytagname ("*"); var allclass = []; var i=0; for (var i=0; i<allclasselem.length; i++) {if (allclasselem[i].classname = = newclassname) {Allclass.push (allclasselem[i]);}} return Allclass;} //usage: var Pelementid=document.getelementbyid (" bar "); var buttons = getclassname (Pelementid, "SCO"); //take id=" bar "under class=" SCO "element;          
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
Getelementsbyclassname ()
/*** gets the collection of element objects for the specified class name *@param {Object} node Parent *@param {String} classname Specifies the class name *@return {[Object]} target object collection */function Getelementsbyclassname (node,classname) {If browser support is used directlyif (node.getelementsbyclassname) {return node.getelementsbyclassname (classname);} else {return (function Getelementsbyclass (searchClass,node) {if (node = = null) node = document; var classelements = [], els = node.getelementsbytagname ( "*"), Elslen = els.length, pattern = new RegExp ( "(^|\\s)" +searchclass +for (i = 0, j = 0; i < ElsLen; i++) {if (Pattern.test (els[i].classname)) {Classelements[j] = Els[i]; j + +;}} return classelements;}) (classname, node); }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
Addloadevent ()
/*** 方便的将在文档加载后运行的函数添加到window.onload中* @param {function} func 待运行函数*/function addLoadEvent(func){    var oldOnload = window.onload;    //typeof 返回String类型    if(typeof window.onload != ‘function‘){ window.onload = func; }else{ window.onload = function(){ oldOnload(); func(); } }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
AddClass ()
/*** 为指定元素结点添加新类名* element 元素结点* value 类名*/function addClass(element,value){//如果元素类名为空,直接将value赋值给相应的元素类名    if(!element.className){        element.className = value;    }else{    //否则需要将类名之间用空格隔开 newClassName = element.className; //多个类名间添加空格 newClassName += ‘ ‘; newClassName += value; element.className = newClassName; }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
InsertAfter ()
/*** 在目标元素结点后面插入新的元素结点* newElement 待插入的新元素结点* targetElement 目标元素结点*/function insertAfter(newElement,targetElement){    var parent = targetElement.parentNode;    if(parent.lastChild == targetElement){        parent.appendChild(newElement);    }else{        parent.insertBefore(newElement,targetElement.nextSibling);    }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
Getnextelement ()
/*** 获取下一个元素结点* @param {Object} node  兄结点* @return {Object || null}下一个元素结点或未获取到*/function getNextElement(node){        var sibNode = node.nextSibling;    if (sibNode.nodeType == 1) { return node; } if (sibNode.nextSibling) { //递归调用 return getNextElement(node.nextSibling); } return null;}

Native JS implements several common DOM manipulation APIs

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.