Is it possible to append an element to a JavaScript nodeList?

前端 未结 5 1750

I\'m generating content dynamically, so I\'m often ending up with documentFragments which I\'m querying using querySelectorAll or querySelector

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-13 06:01

    To not get into technicalities with array methods it can sometimes be more readable to make a list of node names and loop over the list.

    In this example I assign the same event handler to all buttons in two different radio button groups (where each button in the group has the same name):

    Phase
    Ssr
    Relay
    Manual
    Automatic

    Sample event handler:

    var evtRbtnHandler =
        function rbtnHandler() {
            document.getElementById("response11").innerHTML = this.name + ": " + this.value;
        }
    

    And assigning:

    var buttongroup = ["acmode", "powermode"];
    buttongroup.forEach(function (name) {
        document.getElementsByName(name).forEach(function (elem) {
            elem.onclick = evtRbtnHandler;
        });
    });
    

    In any case once you get your hands on each single item it can be pushed() to an array using human readable code.

提交回复
热议问题