使用JavaScript在div中添加/删除HTML

Dan*_*son 66 html javascript innerhtml

我希望能够在div中添加多行并删除它们.我在页面顶部有一个"+"按钮,用于添加内容.然后在每一行的右边有一个' - '按钮,用于删除那一行.我只是无法弄清楚这个例子中的javascript代码.

这是我的基本HTML结构:

<input type="button" value="+" onclick="addRow()">

<div id="content">

</div>
Run Code Online (Sandbox Code Playgroud)

这是我想在内容div中添加的内容:

<input type="text" name="name" value="" />
<input type="text" name="value" value="" />
<label><input type="checkbox" name="check" value="1" />Checked?</label>
<input type="button" value="-" onclick="removeRow()">
Run Code Online (Sandbox Code Playgroud)

Aus*_*rst 128

你可以做这样的事情.

function addRow() {
  const div = document.createElement('div');

  div.className = 'row';

  div.innerHTML = `
    <input type="text" name="name" value="" />
    <input type="text" name="value" value="" />
    <label> 
      <input type="checkbox" name="check" value="1" /> Checked? 
    </label>
    <input type="button" value="-" onclick="removeRow(this)" />
  `;

  document.getElementById('content').appendChild(div);
}

function removeRow(input) {
  document.getElementById('content').removeChild(input.parentNode);
}
Run Code Online (Sandbox Code Playgroud)

  • 哦,知道了。您必须从父级中删除一个子级,因此请在“ removeRow()”中将“ input.parentNode”更改为“ input.parentNode.parentNode”。提醒您,如果您将更多的父母添加到删除按钮,则必须像我刚才那样更改`removeRow`。 (2认同)

小智 6

另一种解决方案是使用getDocumentByIdinsertAdjacentHTML

代码:

function addRow() {

 const  div = document.getElementById('content');

 div.insertAdjacentHTML('afterbegin', 'PUT_HTML_HERE');

}
Run Code Online (Sandbox Code Playgroud)

检查此处,了解更多详细信息: Element.insertAdjacentHTML()


Hof*_*ann 5

令我最大的惊讶的是,我向您介绍了一个从未使用过的DOM方法,在对这个问题进行解答并insertAdjacentHTMLMDN上找到古老的方法之前(请参阅CanIUse?insertAdjacentHTML以获取漂亮的绿色兼容性表)。

所以用它你会写

function addRow () {
  document.querySelector('#content').insertAdjacentHTML(
    'afterbegin',
    `<div class="row">
      <input type="text" name="name" value="" />
      <input type="text" name="value" value="" />
      <label><input type="checkbox" name="check" value="1" />Checked?</label>
      <input type="button" value="-" onclick="removeRow(this)">
    </div>`      
  )
}

function removeRow (input) {
  input.parentNode.remove()
}
Run Code Online (Sandbox Code Playgroud)
<input type="button" value="+" onclick="addRow()">

<div id="content">
</div>
Run Code Online (Sandbox Code Playgroud)


小智 5

我知道花了太长的时间,这意味着你可以写得更简短一些。

    function addRow() {
        var inputName, inputValue, label, checkBox, checked, inputDecrease, content, Ptag;
        // div
        content = document.getElementById('content');
        // P tag
        Ptag = document.createElement('p');
        // first input
        inputName = document.createElement('input');
        inputName.type = 'text';
        inputName.name = 'name';
        // Second input
        inputValue = document.createElement('input');
        inputValue.type = 'text';
        inputValue.name = 'Value';
        // Label
        label = document.createElement('label');
        // checkBox
        checkBox = document.createElement('input');
        checkBox.type = 'checkbox';
        checkBox.name = 'check';
        checkBox.value = '1';
        // Checked?
        checked = document.createTextNode('Checked?');
        // inputDecrease
        inputDecrease = document.createElement('input');
        inputDecrease.type = 'button';
        inputDecrease.value = '-';
        inputDecrease.setAttribute('onclick', 'removeRow(this)')
        // Put in each other
        label.appendChild(checkBox);
        label.appendChild(checked);
        Ptag.appendChild(inputName);
        Ptag.appendChild(inputValue);
        Ptag.appendChild(label);
        Ptag.appendChild(inputDecrease);
        content.appendChild(Ptag);
    }

    function removeRow(input) {
        input.parentNode.remove()
    }
Run Code Online (Sandbox Code Playgroud)
* {
    margin: 3px 5px;
}
Run Code Online (Sandbox Code Playgroud)
<input type="button" value="+" onclick="addRow()">

<div id="content">

</div>
Run Code Online (Sandbox Code Playgroud)