使用JavaScript动态添加HTML中的元素,为每个部分添加元素。

huangapple go评论81阅读模式
英文:

Adding elements dynamically in HTML for each section with JavaScript

问题

以下是您要的代码部分的中文翻译:

添加任务(输入+标签),当我输入名称并按下“添加”按钮时,为每个特定的部分/分区动态添加任务。

我尝试为每个按钮创建一个事件,然后为该特定部分的特定部分添加任务,但我无法实现它。是否有解决方案?

这是HTML:

<section id="1" class="1">
    <div id="div1" class="add-input-label">
        <input type="checkbox" name="checkbox-1" id="checkbox-1">
        <label for="checkbox-1">测试</label>
    </div>
    <div id="btn-div-1" class="btn-div-1">
        <input type="text" id="input-text-1">
        <button id="btn-1" class="btn-add">添加</button>
    </div>
</section>
<section id="2" class="2">
    <div id="div2" class="add-input-label">
        <input type="checkbox" name="checkbox-2" id="checkbox-2">
        <label for="checkbox-2">测试</label>
    </div>
    <div id="btn-div-2" class="btn-div-2">
        <input type="text" id="input-text-2">
        <button id="btn-2" class="btn-add">添加</button>
    </div>
</section>

这是JavaScript:

const btn = document.querySelectorAll('.btn-add');
const div = document.querySelectorAll('.add-input-label');

btn.forEach((btn) => {
    btn.addEventListener('click', () => {
        div.forEach((div) => {
            let label = document.createElement('label');
            let input = document.createElement('input');
            div.appendChild(input);
            div.appendChild(label);
            label.innerText = '嘿';
            input.type = 'checkbox';
        });
    });
});

请注意,当单击按钮时,它会在两个部分中添加。如果我点击第一部分的按钮,我希望只更改第一部分。

英文:

Adding a task (input + label) dynamically for each specific section/div when I enter the name and press the button "Add".

I've tried to make an event for each button and then add a task for the specific div of that specific section but I couldn't implement it. Is there any solution for this?

This is the HTML:

&lt;section id=&quot;1&quot; class=&quot;1&quot;&gt;
    &lt;div id=&quot;div1&quot; class=&quot;add-input-label&quot;&gt;
        &lt;input type=&quot;checkbox&quot; name=&quot;checkbox-1&quot; id=&quot;checkbox-1&quot;&gt;
        &lt;label for=&quot;checkbox-1&quot;&gt;Test&lt;/label&gt;
    &lt;/div&gt;
    &lt;div id=&quot;btn-div-1&quot; class=&quot;btn-div-1&quot;&gt;
        &lt;input type=&quot;text&quot; id=&quot;input-text-1&quot;&gt;
        &lt;button id=&quot;btn-1&quot; class=&quot;btn-add&quot;&gt;Add&lt;/button&gt;
    &lt;/div&gt;
&lt;/section&gt;
&lt;section id=&quot;2&quot; class=&quot;2&quot;&gt;
    &lt;div id=&quot;div2&quot; class=&quot;add-input-label&quot;&gt;
        &lt;input type=&quot;checkbox&quot; name=&quot;checkbox-2&quot; id=&quot;checkbox-2&quot;&gt;
        &lt;label for=&quot;checkbox-2&quot;&gt;Test&lt;/label&gt;
    &lt;/div&gt;
    &lt;div id=&quot;btn-div-2&quot; class=&quot;btn-div-2&quot;&gt;
        &lt;input type=&quot;text&quot; id=&quot;input-text-2&quot;&gt;
        &lt;button id=&quot;btn-2&quot; class=&quot;btn-add&quot;&gt;Add&lt;/button&gt;
    &lt;/div&gt;
&lt;/section&gt;

and this is the Javascript:

const btn = document.querySelectorAll(&#39;.btn-add&#39;);
const div = document.querySelectorAll(&#39;.add-input-label&#39;);

btn.forEach((btn) =&gt; {
btn.addEventListener(&#39;click&#39;, () =&gt; {
    div.forEach((div) =&gt; {
        let label = document.createElement(&#39;label&#39;);
        let input = document.createElement(&#39;input&#39;);
        div.appendChild(input);
        div.appendChild(label);
        label.innerText = &#39;hey&#39;;
        input.type = &#39;checkbox&#39;;
    });
});

});

Notice that when you click the button, it adds on both sections. If I clicked on the first section button, I wanted to change just the first section.

答案1

得分: 0

以下是翻译好的部分:

  1. 利用 forEach 索引语法。在 btn.forEach((btn,i) 中的索引 i 可以用来区分按钮的索引。
  2. 你不需要在 div 上再次使用 forEach。在按钮点击监听器中,只需使用之前获取的按钮索引,并在 div 上执行相同的索引操作,以确保 appendChild 将附加到正确的 div 上。
英文:

There are few changes you need to made on your code to achieve what you wanted.

  1. Make use of forEach index syntax. With the index i in btn.forEach((btn,i), you can use it to differentiate on the button's index.
  2. You dont need another forEach on the div. In button click listener, just use the button index you got earlier and perform the same indexing on the div to make sure that appendChild is appending on the correct div.

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

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

const btn = document.querySelectorAll(&#39;.btn-add&#39;);
const div = document.querySelectorAll(&#39;.add-input-label&#39;);
btn.forEach((btn,i) =&gt; {
  btn.addEventListener(&#39;click&#39;, () =&gt; {
      let label = document.createElement(&#39;label&#39;);
      let input = document.createElement(&#39;input&#39;);
      div[i].appendChild(input);
      div[i].appendChild(label);
      label.innerText = &#39;hey&#39;;
      input.type = &#39;checkbox&#39;;
  });
});

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

&lt;section id=&quot;1&quot; class=&quot;1&quot;&gt;
    &lt;div id=&quot;div1&quot; class=&quot;add-input-label&quot;&gt;
        &lt;input type=&quot;checkbox&quot; name=&quot;checkbox-1&quot; id=&quot;checkbox-1&quot;&gt;
        &lt;label for=&quot;checkbox-1&quot;&gt;Test&lt;/label&gt;
    &lt;/div&gt;
    &lt;div id=&quot;btn-div-1&quot; class=&quot;btn-div-1&quot;&gt;
        &lt;input type=&quot;text&quot; id=&quot;input-text-1&quot;&gt;
        &lt;button id=&quot;btn-1&quot; class=&quot;btn-add&quot;&gt;Add&lt;/button&gt;
    &lt;/div&gt;
&lt;/section&gt;
&lt;section id=&quot;2&quot; class=&quot;2&quot;&gt;
    &lt;div id=&quot;div2&quot; class=&quot;add-input-label&quot;&gt;
        &lt;input type=&quot;checkbox&quot; name=&quot;checkbox-2&quot; id=&quot;checkbox-2&quot;&gt;
        &lt;label for=&quot;checkbox-2&quot;&gt;Test&lt;/label&gt;
    &lt;/div&gt;
    &lt;div id=&quot;btn-div-2&quot; class=&quot;btn-div-2&quot;&gt;
        &lt;input type=&quot;text&quot; id=&quot;input-text-2&quot;&gt;
        &lt;button id=&quot;btn-2&quot; class=&quot;btn-add&quot;&gt;Add&lt;/button&gt;
    &lt;/div&gt;
&lt;/section&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月23日 23:55:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547248.html
匿名

发表评论

匿名网友

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

确定