Javascript 通过div jquery中的类重申
假设我有以下结构:Javascript 通过div jquery中的类重申,javascript,jquery,iteration,Javascript,Jquery,Iteration,假设我有以下结构: <div id="table"> <div class="row"> <div class="cell">a</div> <div class="cell">b</div> </div> <div class="row"> <div class="cell">d</div> <div class=
<div id="table">
<div class="row">
<div class="cell">a</div>
<div class="cell">b</div>
</div>
<div class="row">
<div class="cell">d</div>
<div class="cell">c</div>
</div>
</div>
A.
B
D
C
我如何通过它们重申,我创建了相同的结构,但使用了
而不是
,
而不是
,
,使用div的内容而不是
我试着做一些类似的事情
let topics_table = '<table>'
$('#table > .row').each(function(){
topics_table += '<tr>'
$(this + ' .cell').each(function(that){
topics_table += '<td>' + that.text() + '</td>'
})
topics_table += '</tr>';
})
topics_table += '</table>`
let topics_table=''
$('#table>.row')。每个(函数(){
主题\u表+=“”
$(this+'.cell')。每个(函数(that){
主题\u table+=''+that.text()+''
})
主题_表+='';
})
主题\u表+='`
但我有个错误
$(this + ' .cell')
应该是
$(this).find('.cell')
// or
$('.cell', this)
您正在尝试将字符串连接到DOM元素
.each(function(that){
topics_table += '<td>' + that.text() + '</td>'
})
。每个(函数){
主题\u table+=''+that.text()+''
})
^^这也是一个问题,因为jQuery将索引作为第一个参数而不是元素传入
.each(function(index, that){
topics_table += '<td>' + that.innerText + '</td>'
})
.each(函数(索引,即){
主题\u table+=''+that.innerText+''
})