简体   繁体   English

我需要创建带有嵌套div的div

[英]I need to create divs with nested divs inside it

have a look at the code 看一下代码

Html Code : HTML代码:

<div class="container content-rows" id="contentdisplay">
 <div class="col-md-1" id="snocontent">abcd</div>
 <div class="col-md-3" id="pgnamecontent">abcd</div>
 <div class="col-md-3" id="cmpcontent">abcd</div>
 <div class="col-md-2" id="datecontent">abcd</div>
 <div>
  <button onclick="createdivs()"><span class="glyphicon glyphicon-king" class="addtrainersbutton" id="addtrainersbutton" title="Add Trainers"></span></button>
  <button onclick="edit_program()"><span class="glyphicon glyphicon-pencil" id="editprogrambutton" title="Edit Program"></span></button>
  <span class="glyphicon glyphicon-user" id="assigntrainersbutton" title="Assign Trainers for the Program"></span>
  <span class="glyphicon glyphicon-remove" id="deleteprogrambutton" title="Delete the Program"></span>
 </div>

Javascript Code: JavaScript代码:

function createdivs() {
        var i;
    for (i = 0;i < 10;i++)
    {
        divc = "<div>.container.content-rows";
        var list = document.createElement("divc");
        document.body.appendChild(list);
        list.className = "proglist";

    }
}

I have a few questions, please clarify them or explain with code: 我有几个问题,请澄清或用代码解释:

  1. 10 divc's are created but there are no other div elements inside them, only the first div has some content in it.. other divs are just created and they dnt even occupy space inside the webpage 创建了10个divc,但其中没有其他div元素,只有第一个div包含一些内容。.其他div才被创建,它们甚至不占用网页内的空间

  2. I want the div to be aligned to the first div, ie., instead of document.body.appendChild I need something like document.div.appendChild, whereas the created divs should be appeneded with the first div.. 我希望div与第一个div对齐,即,而不是document.body.appendChild,我需要像document.div.appendChild之类的东西,而创建的div应该与第一个div附加。

Please let me know how can I get them with explanation.. Thank you in advance.. 请让我知道如何获得解释。.预先谢谢..

10 divc's are created but there are no other div elements inside them, only the first div has some content in it.. other divs are just created and they dnt even occupy space inside the webpage 创建了10个divc,但其中没有其他div元素,只有第一个div包含一些内容。.其他div才被创建,它们甚至不占用网页内的空间

Space isn't occupied because they don't have any content added to them. 没有占用空间,因为它们没有添加任何内容。 You add content to an element just like you do with document.body - appendChild() for example. 您可以像对document.body - appendChild()一样向元素添加内容。

I want the div to be aligned to the first div, ie., instead of document.body.appendChild I need something like document.div.appendChild, whereas the created divs should be appeneded with the first div.. 我希望div与第一个div对齐,即,而不是document.body.appendChild,我需要像document.div.appendChild之类的东西,而创建的div应该与第一个div附加。

Elements have such a method: 元素有这样一种方法:

  function createdivs() { var i; for (i = 0; i < 10; i++) { divc = "<div>.container.content-rows"; var divc = document.createElement("div"); divc.classList.add("container"); divc.classList.add("content-rows"); divc.classList.add("proglist"); divc.textContent = "I'm div #" + i; document.getElementById('contentdisplay').appendChild(divc); createNestedDivs(divc); } } function createNestedDivs(selector) { function appendToNode(node, content) { // ideally content would also be a Node, but for simplicity, // I'm assuming it's a string. var inner = document.createElement('span'); inner.textContent = content; node.appendChild(inner); } if (selector instanceof Node) { appendToNode(selector, "inner"); return; } var selected = Array.prototype.slice.call(document.querySelectorAll(selector)); selected.forEach(function(el) { appendToNode(el, "inner"); }); } 
 <div class="container content-rows" id="contentdisplay"> <div class="col-md-1" id="snocontent">abcd</div> <div class="col-md-3" id="pgnamecontent">abcd</div> <div class="col-md-3" id="cmpcontent">abcd</div> <div class="col-md-2" id="datecontent">abcd</div> <div> <button onclick="createdivs()"><span class="glyphicon glyphicon-king" class="addtrainersbutton" id="addtrainersbutton" title="Add Trainers"></span> </button> <button onclick="edit_program()"><span class="glyphicon glyphicon-pencil" id="editprogrambutton" title="Edit Program"></span> </button> <span class="glyphicon glyphicon-user" id="assigntrainersbutton" title="Assign Trainers for the Program"></span> <span class="glyphicon glyphicon-remove" id="deleteprogrambutton" title="Delete the Program"></span> </div> 

The createNestedDivs() function takes care of if you want to append another child to each created element. 如果要将另一个子项附加到每个创建的元素上,则createNestedDivs()函数会负责。 You can pass it a Node or string and it will treat them as expected. 您可以向其传递一个Nodestring ,它将按预期对待它们。 createNestedDivs() works by the same principles as the modifications I made to createdivs() . createNestedDivs()工作原理与我对createdivs()所做的修改相同。 The case for handling a Node is simple enough, but the string handling is a little less clear: 处理Node的情况很简单,但是string处理却不太清楚:

  • The Array.prototype.slice.call is necessary because document.querySelectorAll returns a NodeList , which isn't an array, and I can't use Array.prototype.forEach() on it. Array.prototype.slice.call是必需的,因为document.querySelectorAll返回一个NodeList ,它不是一个数组,并且我不能在其上使用Array.prototype.forEach()

Also, related reading here: http://www.w3.org/wiki/The_principles_of_unobtrusive_JavaScript 另外,此处的相关阅读内容: http : //www.w3.org/wiki/The_principles_of_unobtrusive_JavaScript

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

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