Append multiple items in JavaScript

后端 未结 9 413
长情又很酷
长情又很酷 2020-12-07 20:58

I have the following function and I am trying to figure out a better way to append multiple items using appendChild().

When the user clicks on Add, each

9条回答
  •  青春惊慌失措
    2020-12-07 21:28

    You can do it with DocumentFragment.

    var documentFragment = document.createDocumentFragment();
    documentFragment.appendChild(listItem);
    listItem.appendChild(listItemCheckbox);
    listItem.appendChild(listItemLabel);
    listItem.appendChild(editButton);
    listItem.appendChild(deleteButton);
    listElement.appendChild(documentFragment);
    

    DocumentFragments allow developers to place child elements onto an arbitrary node-like parent, allowing for node-like interactions without a true root node. Doing so allows developers to produce structure without doing so within the visible DOM

提交回复
热议问题