Javascript 如何用JS将HTML重写为HTML?
在应用程序切换到REST/AJAX之前,我有以下html代码:Javascript 如何用JS将HTML重写为HTML?,javascript,html,Javascript,Html,在应用程序切换到REST/AJAX之前,我有以下html代码: <div class="container"> <h2 align="center">TEACHERS</h2> <table class="table table-dark" border="1" width="100%" cellpadding="5">
<div class="container">
<h2 align="center">TEACHERS</h2>
<table class="table table-dark" border="1" width="100%" cellpadding="5">
<thead>
<tr>
<th> ID</th>
<th> Name</th>
<th> Position</th>
</tr>
</thead>
<tbody>
<tr th:if="${teachers.empty}">
<td colspan="2"> No teachers Available</td>
</tr>
<tr th:each="teacher : ${teachers}">
<td><a th:href="@{/teacherViews/teacherDescription(teacherId=${teacher.teacherId})}"><span
th:text="${teacher.teacherId}"></span></a></td>
<td><span th:text="${teacher.teacherName}"></span></td>
<td><span th:text="${teacher.position}"></span></td>
</tr>
</tbody>
</table>
teachers.js
:
var service = 'http://localhost:8081/';
$(document).ready(function(){
jQuery.support.cors = true;
$.ajax(
{
type: "GET",
url: service + 'check/',
// data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (data) {
var trHTML = '';
$.each(data, function (i, teacher) {
trHTML += '<tr><td>' + teacher.teacherId + '</td><td>' + teacher.teacherName + '</td><td>' + teacher.position + '</td></tr>';
});
$('#table-content').append(trHTML);
},
error: function (msg) {
alert(msg.responseText);
}
});
})
var服务=”http://localhost:8081/';
$(文档).ready(函数(){
jQuery.support.cors=true;
$.ajax(
{
键入:“获取”,
url:service+'check/',
//数据:“{}”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
cache:false,
成功:功能(数据){
var trHTML='';
$。每个(数据、函数(i、教师){
trHTML+=''+teacher.teacherId+''+teacher.teacherName+''+teacher.position+'';
});
$(“#表内容”).append(trHTML);
},
错误:函数(msg){
警报(msg.responseText);
}
});
})
这段代码运行良好,页面上有表显示
据我所知,我需要重写这一行
trHTML+=''+teacher.teacherId+''+teacher.teacherName+''+teacher.position+'';})
在teachers.js中
那么你想在你的教师id上有一个按钮来显示信息吗?@Alex是的,我想“teacherId”是可点击的,点击后可以重定向到另一个页面所有到同一页面?@Alex点击“teacherId”后我需要在传递的“teacherId”的帮助下显示关于这位教师的信息。为此,我想在同一窗口中打开另一个html页面。
var service = 'http://localhost:8081/';
$(document).ready(function(){
jQuery.support.cors = true;
$.ajax(
{
type: "GET",
url: service + 'check/',
// data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (data) {
var trHTML = '';
$.each(data, function (i, teacher) {
trHTML += '<tr><td>' + teacher.teacherId + '</td><td>' + teacher.teacherName + '</td><td>' + teacher.position + '</td></tr>';
});
$('#table-content').append(trHTML);
},
error: function (msg) {
alert(msg.responseText);
}
});
})