Wrap every 2 elements in a
  • tag with js vanilla?
  • huangapple go评论74阅读模式
    英文:

    Wrap every 2 elements in a <li> tag with js vanilla?

    问题

    const items = [...document.querySelectorAll('ul li')];
    for (let i = 0; i < items.length; i += 2) {
        const wrapper = document.createElement('div');
        items.slice(i, i += 4);
        items.parentNode.insertBefore(wrapper, items);
        wrapper.appendChild(items);
    }
    
    英文:

    I would like to put every two li elements into a div in vanilla js. But I don't know wher is problem.

    Thnks for help!

    &lt;ul&gt;
       &lt;li&gt;&lt;/li&gt;
       &lt;li&gt;&lt;/li&gt;
       &lt;li&gt;&lt;/li&gt;
       &lt;li&gt;&lt;/li&gt;
       &lt;li&gt;&lt;/li&gt;
       &lt;li&gt;&lt;/li&gt;
    &lt;/ul&gt;
    
    
    const items = [...document.querySelectorAll(&#39;ul li&#39;)];
    for (let i = 0; i &lt; items.length; i += 2) {
        const wrapper = document.createElement(&#39;div&#39;);
        items.slice(i, i += 4);
        items.parentNode.insertBefore(wrapper, items);
        wrapper.appendChild(items);
    }
    

    答案1

    得分: 1

    你可以使用 document.createElement 在每次迭代中创建一个容器,然后将一些现有元素附加到它上面。

    const container = document.getElementById('container');
    const paragraphs = [...container.querySelectorAll('p')]; // 或者使用 container.children
    let incr = 2;
    for (let i = 0; i < paragraphs.length; i += incr) {
      const div = document.createElement('div');
      for (const p of paragraphs.slice(i, i + incr)) div.append(p);
      container.append(div);
    }
    
    #container > div {
      border: 1px solid;
    }
    
    <div id="container">
       <p>文本</p>
       <p>文本</p>
       <p>文本</p>
       <p>文本</p>
       <p>文本</p>
       <p>文本</p>
    </div>
    
    英文:

    You can use document.createElement to make a container on each iteration, then append some of the existing elements to it.

    <!-- begin snippet: js hide: false console: true babel: false -->

    <!-- language: lang-js -->

    const container = document.getElementById(&#39;container&#39;);
    const paragraphs = [...container.querySelectorAll(&#39;p&#39;)]; // or container.children
    let incr = 2;
    for (let i = 0; i &lt; paragraphs.length; i += incr) {
      const div = document.createElement(&#39;div&#39;);
      for (const p of paragraphs.slice(i, i + incr)) div.append(p);
      container.append(div);
    }
    

    <!-- language: lang-css -->

    #container &gt; div {
      border: 1px solid;
    }
    

    <!-- language: lang-html -->

    &lt;div id=&quot;container&quot;&gt;
       &lt;p&gt;text&lt;/p&gt;
       &lt;p&gt;text&lt;/p&gt;
       &lt;p&gt;text&lt;/p&gt;
       &lt;p&gt;text&lt;/p&gt;
       &lt;p&gt;text&lt;/p&gt;
       &lt;p&gt;text&lt;/p&gt;
    &lt;/div&gt;
    

    <!-- end snippet -->

    huangapple
    • 本文由 发表于 2023年2月9日 03:37:24
    • 转载请务必保留本文链接:https://go.coder-hub.com/75390913.html
    匿名

    发表评论

    匿名网友

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    确定