Javascript 将一个div环绕在;document.createElement";
这似乎是个小问题,但我找不到解决办法Javascript 将一个div环绕在;document.createElement";,javascript,css,Javascript,Css,这似乎是个小问题,但我找不到解决办法 tableAdd: function(entry) { var $tr = document.createElement('div'), $td, key; $tr.setAttribute("class", "mylist_zeile clearfix"); for (key in entry) { if (entry.hasOwnProperty(key)) { $td = docume
tableAdd: function(entry) {
var $tr = document.createElement('div'), $td, key;
$tr.setAttribute("class", "mylist_zeile clearfix");
for (key in entry) {
if (entry.hasOwnProperty(key)) {
$td = document.createElement("div");
$td.appendChild(document.createTextNode(entry[key]));
$tr.appendChild($td);
$td.setAttribute("class", "mylist_spalte");
}
}
$td = document.createElement("span");
$td.setAttribute("class", "mylist_remove");
$td.innerHTML = '<a data-op="remove" data-id="'+ entry.id +'">X</a>';
$tr.appendChild($td);
$tr.setAttribute("id", "entry-"+ entry.id);
mylist.$table.appendChild($tr);
}
tableAdd:函数(条目){
var$tr=document.createElement('div'),$td,key;
$tr.setAttribute(“类”、“mylist_zeile clearfix”);
用于(输入项){
if(条目.hasOwnProperty(键)){
$td=document.createElement(“div”);
$td.appendChild(document.createTextNode(条目[key]);
$tr.appendChild($td);
$td.setAttribute(“类”、“mylist_spalte”);
}
}
$td=document.createElement(“span”);
$td.setAttribute(“类”、“mylist_remove”);
$td.innerHTML='X';
$tr.appendChild($td);
$tr.setAttribute(“id”、“条目-”+entry.id);
mylist.$table.appendChild($tr);
}
…给了我:
<div id="entry-1" class="mylist_zeile clearfix">
<div class="mylist_spalte">1</div>
<div class="mylist_spalte">Title One</div>
<div class="mylist_spalte">http://www.google.com/someurl1</div>
<span class="mylist_remove">
<a data-id="1" data-op="remove">X</a>
</span>
</div>
<div id="entry-2" class="mylist_zeile clearfix">
<div class="mylist_spalte">2</div>
<div class="mylist_spalte">Title Two</div>
<div class="mylist_spalte">http://www.google.com/someurl2</div>
<span class="mylist_remove">
<a data-id="2" data-op="remove">X</a>
</span>
</div>
1.
标题一
http://www.google.com/someurl1
X
2.
标题二
http://www.google.com/someurl2
X
到现在为止还不错。现在我需要用另一个div(用类“mylist\u wrapper”
)用类“mylist\u wrapper”
将这3个div包装起来。每个用类“mylist\u spalte”
的div都应该得到另一个类:
第一部分:“第1行”
第二部分:“第2行”
第三部分:“第3行”
有什么想法吗?你可以这样做
var $parentdiv = document.createElement('div');
var $tr = document.createElement('div'), $td, key;
// in the end of $tr assignment/operations
$parentdiv.appendChild($tr);
mylist.$table.appendChild($tr);
您可以使用classList对象代替setAttribute。 然后将所有内容包装到另一个div中,这应该非常简单,请尝试以下代码
tableAdd: function(entry) {
var $tr = document.createElement('div'),
$wrap = document.createElement('div'),
$td, key;
$tr.setAttribute("class", "mylist_zeile clearfix");
$wrap.setAttribute("class", "mylist_wrapper");
var count = 0;
for (key in entry) {
if (entry.hasOwnProperty(key)) {
count++;
$td = document.createElement("div");
$td.appendChild(document.createTextNode(entry[key]));
$td.classList.add("mylist_spalte");
$td.classList.add("row_"+count);
$wrap.appendChild($td);
}
}
$tr.appendChild($wrap);
$td = document.createElement("span");
$td.setAttribute("class", "mylist_remove");
$td.innerHTML = '<a data-op="remove" data-id="'+ entry.id +'">X</a>';
$tr.appendChild($td);
$tr.setAttribute("id", "entry-"+ entry.id);
mylist.$table.appendChild($tr);
}
tableAdd:函数(条目){
var$tr=document.createElement('div'),
$wrap=document.createElement('div'),
$td,钥匙;
$tr.setAttribute(“类”、“mylist_zeile clearfix”);
$wrap.setAttribute(“类”、“mylist_包装器”);
var计数=0;
用于(输入项){
if(条目.hasOwnProperty(键)){
计数++;
$td=document.createElement(“div”);
$td.appendChild(document.createTextNode(条目[key]);
$td.classList.add(“mylist_spalte”);
$td.classList.add(“行”+计数);
$wrap.appendChild($td);
}
}
$tr.appendChild($wrap);
$td=document.createElement(“span”);
$td.setAttribute(“类”、“mylist_remove”);
$td.innerHTML='X';
$tr.appendChild($td);
$tr.setAttribute(“id”、“条目-”+entry.id);
mylist.$table.appendChild($tr);
}
如果你把问题简化成一个简单的例子,你可能会得到更多的答案。Thx@Bolza!很有魅力!太棒了!