简体   繁体   English

JS将子级附加到类的每个元素

[英]JS Append child to each element of class

function makeCategory() {
    getList = document.getElementById("list");
    cat = document.createElement("div");
    cat.setAttribute("class", "cat");
    getList.appendChild(cat);
    cat.innerHTML =
        '<input type="text" name="name"/><span class="removeButton" onclick= "remove(this)">-</span><br><span class="makeSubCat" onclick="makeSubCategory(this)">+Category</span>';
}

function remove(z) {
    document.getElementById("list").removeChild(z.parentNode);
}

function makeSubCategory(i) {
    y = document.createElement("input");
    x = document.getElementsByClassName("cat");
    x[i].appendChild(y);
}

Can't figure out how i can append child on each element of the class with "onclick".无法弄清楚如何使用“onclick”在类的每个元素上附加孩子。 It's not working with "for loop" - it's always appends to the latest div.它不适用于“for 循环”——它总是附加到最新的 div。 It's only works when i specify the number of class element.它仅在我指定类元素的数量时才有效。

Demo Outline演示大纲

  • Reworked the HTML tags into a real Description List or <dl>将 HTML 标签改造成真正的描述列表或<dl>
  • Instead of trying to manipulate multiple elements recursively, we will drive the functions by the click event我们将通过单击事件驱动函数,而不是尝试递归地操作多个元素
  • Use of Event Delegation saves us a ton of extra code. 事件委托的使用为我们节省了大量额外的代码。 Only one eventListener() is needed for an unlimited number of <button> s and <input> s.无限数量的<button><input>只需要一个eventListener()
  • Template Literal was used in lieu of string literal使用模板文字代替字符串文字
  • insertAdjacentHTML() played an important part of the add() function insertAdjacentHTML()起到了add()函数的重要作用

Details are commented within the demo详细信息在演示中评论

Demo演示

 // Reference the Description List dl#list var dock = document.getElementById("list"); /* Callback function || if the clicked button is NOT dl#list... || - tgt/e.target is the clicked <button> || - if tgt has .add, then call add()... || else if tgt has .rem... || cat is its parent (.cat) || - Get div.cat parent and remove .cat */ function mod(e, dock) { if (e.target !== e.currentTarget) { var tgt = e.target; if (tgt.className === 'add') { add.call(this, dock); } else if (tgt.className === 'rem') { var cat = tgt.parentNode; cat.parentNode.removeChild(cat); } else return; } } /* Register the ancestor of all of the .cat || and <button>s. (dl#list). || By doing this there's no need to addEventListeners || for every <button> */ dock.addEventListener('click', function(e) { mod(e, this); }, false); /* This function expression takes a string (in this || case the string is a Template Literal.) and || parses it into HTML as it inserts it at a || position determined by the first parameter: || "beforeend" || (exactly like append) */ var add = function(dock) { var cat = `<dd class='cat'> <input name="name" type='text'> <button class="rem">-</button> <button class="add">+</button> </dd>`; dock.insertAdjacentHTML('beforeend', cat); };
 #list { margin: 20px; border: 2px ridge gold; background: rgba(0, 0, 0, .3); padding: 5px 10px 15px; } dt { font: 700 20px/1 Consolas; color: gold; background: rgba(0, 0, 0, .5); padding: 5px; } dd { font-size: 16px; color: #fff; background: rgba(0, 0, 0, .5); padding: 5px; margin: 8px 4px 8px 0; } input, button { width: 10%; font: inherit; display: inline-block; } input[type='text'] { width: 76%; }
 <dl id='list'> <dt>Category List</dt> <dd class='cat'> <input name="name" type='text'> <button class="add">+</button> </dd> </dl>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM