1. <tfoot id='tcWv7'></tfoot>

  2. <legend id='tcWv7'><style id='tcWv7'><dir id='tcWv7'><q id='tcWv7'></q></dir></style></legend>
    1. <small id='tcWv7'></small><noframes id='tcWv7'>

      <i id='tcWv7'><tr id='tcWv7'><dt id='tcWv7'><q id='tcWv7'><span id='tcWv7'><b id='tcWv7'><form id='tcWv7'><ins id='tcWv7'></ins><ul id='tcWv7'></ul><sub id='tcWv7'></sub></form><legend id='tcWv7'></legend><bdo id='tcWv7'><pre id='tcWv7'><center id='tcWv7'></center></pre></bdo></b><th id='tcWv7'></th></span></q></dt></tr></i><div id='tcWv7'><tfoot id='tcWv7'></tfoot><dl id='tcWv7'><fieldset id='tcWv7'></fieldset></dl></div>
        <bdo id='tcWv7'></bdo><ul id='tcWv7'></ul>

    2. 是否可以将元素附加到 JavaScript 节点列表?

      时间:2023-10-20
        <tbody id='hOh5g'></tbody>
      <i id='hOh5g'><tr id='hOh5g'><dt id='hOh5g'><q id='hOh5g'><span id='hOh5g'><b id='hOh5g'><form id='hOh5g'><ins id='hOh5g'></ins><ul id='hOh5g'></ul><sub id='hOh5g'></sub></form><legend id='hOh5g'></legend><bdo id='hOh5g'><pre id='hOh5g'><center id='hOh5g'></center></pre></bdo></b><th id='hOh5g'></th></span></q></dt></tr></i><div id='hOh5g'><tfoot id='hOh5g'></tfoot><dl id='hOh5g'><fieldset id='hOh5g'></fieldset></dl></div>
      <tfoot id='hOh5g'></tfoot>
        <bdo id='hOh5g'></bdo><ul id='hOh5g'></ul>

          <small id='hOh5g'></small><noframes id='hOh5g'>

        1. <legend id='hOh5g'><style id='hOh5g'><dir id='hOh5g'><q id='hOh5g'></q></dir></style></legend>

              • 本文介绍了是否可以将元素附加到 JavaScript 节点列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在动态生成内容,所以我经常以 documentFragments 结束,我正在使用 querySelectorAllquerySelector 进行查询返回我的 documentFragment 中元素的 nodeList.

                I'm generating content dynamically, so I'm often ending up with documentFragments which I'm querying using querySelectorAll or querySelector returning a nodeList of elements inside my documentFragment.

                有时我想将一个项目添加到列表中,但我在网上找不到任何关于这是否可行的信息.

                From time to time I would like to add an item to a list, but I can't find anything online on whether this is even possible.

                我试过这样:

                 document.querySelectorAll(".translate").item(length+1) = document.createElement("div");
                

                还有这个:

                 document.querySelectorAll(".translate").shift(document.createElement("div"));
                

                但两者都不起作用(如预期)

                But both don't work (as expected)

                问题:
                是否可以手动将元素添加到 NodeList?我猜,不是但还是问.

                Question:
                Is it possible to manually add elements to a NodeList? I guess, not but asking nevertheless.

                感谢您提供一些见解?

                编辑:
                所以更多信息:我正在生成一个动态内容块,我想将其附加到我的页面.默认情况下,该块是英文的.由于用户正在查看中文页面,因此我在动态片段上运行翻译器,然后将其附加到 DOM.在我的页面上,我还有一个元素,比如标题,它应该根据添加的动态内容而改变.我的想法是一步完成 = 尝试向我的 nodeList 添加一个元素.但是从现在开始写...我想不可能:-)

                EDIT:
                So more info: I'm generating a block of dynamic content, which I want to append to my page. By default the block is in English. Since the user is viewing the page in Chinese, I'm running a translator on the dynamic fragment, BEFORE appending it to the DOM. On my page, I also have an element, say a title, which should change depending on the dynamic content being added. My idea was to do this in one step = try to add an element to my nodeList. But from writing it now... I guess not possible :-)

                推荐答案

                正如@Sniffer 提到的,NodeLists 是只读的(长度属性和项目).您可以操作它们的所有其他内容,如下所示,但如果您想操作它们,最好将它们转换为数组.

                As @Sniffer mentioned, NodeLists are read-only (both the length property and the items). You can manipulate everything else about them, like shown below, but it's probably better to convert them to arrays instead if you want to manipulate them.

                var list = document.querySelectorAll('div');
                var spans = document.querySelectorAll('span');
                
                push(list, spans);
                forEach(list, console.log); // all divs and spans on the page
                
                function push(list, items) {  
                  Array.prototype.push.apply(list, items);
                  list.length_ = list.length;
                  list.length_ += items.length;
                }
                
                function forEach(list, callback) {
                  for (var i=0; i<list.length_; i++) {
                    callback(list[i]);
                  }
                }
                

                将 NodeList 转换为数组可能会更好(list = Array.prototype.slice(list)).

                It would probably be a better idea to turn the NodeList to an array instead (list = Array.prototype.slice(list)).

                var list = Array.prototype.slice.call(document.querySelectorAll('div'));
                var spans = Array.prototype.slice.call(document.querySelectorAll('span'));
                
                list.push.apply(list, spans);
                console.log(list); // array with all divs and spans on page
                

                这篇关于是否可以将元素附加到 JavaScript 节点列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何用 Javascript 替换和追加 下一篇:jQuery Masonry 和 Ajax-fetching 追加项目导致图像重叠

                相关文章

              • <tfoot id='EbUep'></tfoot>

                    <legend id='EbUep'><style id='EbUep'><dir id='EbUep'><q id='EbUep'></q></dir></style></legend>
                  1. <small id='EbUep'></small><noframes id='EbUep'>

                    <i id='EbUep'><tr id='EbUep'><dt id='EbUep'><q id='EbUep'><span id='EbUep'><b id='EbUep'><form id='EbUep'><ins id='EbUep'></ins><ul id='EbUep'></ul><sub id='EbUep'></sub></form><legend id='EbUep'></legend><bdo id='EbUep'><pre id='EbUep'><center id='EbUep'></center></pre></bdo></b><th id='EbUep'></th></span></q></dt></tr></i><div id='EbUep'><tfoot id='EbUep'></tfoot><dl id='EbUep'><fieldset id='EbUep'></fieldset></dl></div>
                      <bdo id='EbUep'></bdo><ul id='EbUep'></ul>