[我有 具有相同类别的按钮的框。在单击按钮时,应将一个文本框添加到相应的框中

问题描述 投票:0回答:1

On pressing the second 'Add Task' button, a textbox should be added to the second div, similary with 3rd, 4th 如何引用特定的div元素?无论下面按下哪个按钮,我在下面所做的操作都会将文本框添加到第一个div。

HTML

<body>
    <div id="project">
      LIST IT
    </div>
    <div id="board">
      Java Project
    </div>
  <template id="listTemp">
    <form class="frm" >
      <input type="text" placeholder="Enter list name">
    </form>
    <button type="button" name="task" class="lst btn btn-secondary" onclick="addTask()">Add Task</button>
  </template>
    <button type="button" name="list" class="btn btn-secondary" onclick="addList()">Add List</button>
  </body>
  <script src="index.js" charset="utf-8"></script>

JAVASCRIPT

function addTask(){
  var input = document.createElement('input');
  input.placeholder="Enter task here";
  input.type="text";
  var br = document.createElement('br');
  var parent = document.getElementsByClassName('frm')[0];
  // parent.insertBefore(input,document.getElementsByClassName('lst')[0]);
  parent.appendChild(input);

}
function addList(){
  var div = document.createElement('div');
  div.innerHTML = document.getElementById('listTemp').innerHTML;
  div.className = 'ListBox';
  document.getElementsByTagName('body')[0].appendChild(div);
}
javascript html dom
1个回答
0
投票

要引用div#project,您可以使用document.querySelector('#project');

© www.soinside.com 2019 - 2024. All rights reserved.