There are some incomprehensible problems in native js imitating append ().

when I have nothing to do at work, I want to make my own jquery, to meet my development needs.
when dealing with the append () method, considering that there may be multiple element nodes with duplicate names in class, traversing and adding them requires the addition of element nodes.

js is shown below, appending element nodes through appendChild ()

function EQuery(ele){
    this.elements = [];
    switch(typeof ele){
        case "string":
            switch(ele.charAt(0)){
                case "-sharp": //id
                    this.elements.push(document.querySelector(ele));
                    break;
                case ".": //class
                    this.elements = document.querySelectorAll(ele);
                    break;
                default: // p
                    this.elements = document.getElementsByTagName(ele);
                    break;
            }
            break;    
        default:
            console.log("this type is not supported by EQuery");
            break;
    }    
}

// EQuery.prototype.append = function(obj){  
//     var len = this.elements.length;
//     for (var i = 0; i < len; iPP) {
//         this.elements[i].innerHTML += obj;
//     }
// }   

EQuery.prototype.append = function(obj){
    var len = this.elements.length,
        content = "",
        tagName,
        ele;
    var reg = /<(\S*?)>(\S*?)<\S*?>/;
    if(reg.exec(obj)){
        tagName = RegExp.$1;
        content = RegExp.$2;
    }
    ele = document.createElement(tagName);
    ele.innerHTML = content;
    this.elements.forEach(function(value,index,arr){
        value.appendChild(ele);
    })
}

html

    <div class="spring"><h4>HI</h4></div>
    <div class="spring"><h4>HI</h4></div>
    <div class="spring"><h4>HI</h4></div>
    <div class="spring"><h4>HI</h4></div>
    <div class="spring"><h4>HI</h4></div>
    <script>
    var springList = $E(".spring");
    springList.append("<span>bangbangbang!</span>");
    <script>

in html, all div with class = "spring" are appended with" bangbangbang! ", but only the last div can be successfully added when reviewing the element node, as shown in

printing all the data is normal. The problem should be in the step of adding in the loop. I don"t know what went wrong. Please don"t hesitate to give me advice.

Mar.19,2021

if the inserted node already exists in the document tree of the current document, that node is first removed from the previous location and then inserted into the new location.

if you need to keep the child node displayed in the original location, you need to copy a copy of the node with the Node.cloneNode method and then insert it into the new location.

    this.elements.forEach(function(value,index,arr){
        var ele = document.createElement(tagName);
        ele.innerHTML = content;
        value.appendChild(ele);
    })

appendChild


Hi, multiple appendChild a node will remove from the same place, you can clone node. value.appendChild (ele.cloneNode ());

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b35e15-2bfb0.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b35e15-2bfb0.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?