HTML:
<table class="table table-bordered table-hover">
<thead>
<tr class="warning">
<th> Date </th>
<th> Teacher </th>
<th> Join conference </th>
</tr>
</thead>
<tbody id="lessons_tbody"></tbody>
</table>
使用Jquery 3.2.1,我有以下代码:
$.each(ev, function(key,lesson) {
$('<tr class="active">').appendTo('#lessons_tbody');
$.each(lesson, function(key2, value){
if(key2=='url')
$('#lessons_tbody').append('<td><a href="'+value+'"><button class="btn btn-primary">Launch Lesson</button></a></td>');
else
$('#lessons_tbody').append('<td>'+value+'</td>');
});
$('</tr>').appendTo('#lessons_tbody');
});
这是生成的HTML:
<tbody id="lessons_tbody">
<tr class="active"></tr>
<td>2020-06-23 12:00:00</td>
<td>Testi Test</td>
<td><a href="/conference/lesson/47/"><button class="btn btn-primary">Launch Lesson</button></a></td>
</tbody>
我需要将td单元格放在表格行元素中(...)
编辑1-Json回应:
您可以append
使用创建在某些变量中的html +=
,最后将其附加到tbody
。
演示代码:
//your response
var ev = [{
lessonDate: " 2020-08-12 12:40",
teacher: "abc",
url: "htp://ww.abc.com"
}, {
lessonDate: " 2020-09-12 10:40",
teacher: "abc1",
url: "htp://ww.abc1.com"
}, {
lessonDate: "2020-02-12 12:40",
teacher: "abc2",
url: "htp://ww.abc2.com"
}];
var html = "";
$.each(ev, function(key, lesson) {
//append tr in html variable
html += '<tr class="active">';
$.each(lesson, function(key2, value) {
if (key2 == 'url')
html += '<td><a href="' + value + '"><button class="btn btn-primary">Launch Lesson</button></a></td>';
else
html += '<td>' + value + '</td>';
});
html += '</tr>';
});
$("#lessons_tbody").append(html) //append row to tbody
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<table class="table table-bordered table-hover">
<thead>
<tr class="warning">
<th> Date </th>
<th> Teacher </th>
<th> Join conference </th>
</tr>
</thead>
<tbody id="lessons_tbody"></tbody>
</table>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句