I want to dynamically insert a line of data into tBody, and I write the following code:
html:
<table id="theList">
<thead>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>操作</th>
</thead>
<tbody id="myBody"></tbody>
</table>
js:
let list = []
let temp = []
list.push({
name: '张三',
age: 20,
sex: '男'
})
list.push({
name: '赵四',
age: 19,
sex: '女'
})
function render(data) {
var html=[]
for (let i = 0; i < data.length; i++) {
let template = '<tr><td>'+data[i].name+'</td><td>'+data[i].age+'</td><td>'+data[i].sex+'</td><td><a href="javascript:;">修改</a> <a href="javascript:;">删除</a></td></tr>'
html.push(template)
document.getElementById('myBody').appendChild(html.join(''))
}
}
render(list)
The browser throws an Uncaught TypeError when running the above code: Failed to execute ‘appendChild’ on ‘Node’ : parameter 1 is not of type ‘Node’. To find out the cause of the error by looking up data:
AppendChild () requires that a tr object be passed in, not a tr string
and html.join(“) above is a string
console.log(typeof html.join('')) //stirng
Solution:
render function
function render(data) {
for (let i = 0; i < data.length; i++) {
let tr = document.createElement('tr')
tr.innerHTML = '<td>'+data[i].name+'</td><td>'+data[i].age+'</td><td>'+data[i].sex+'</td><td><a href="javascript:;">修改</a> <a href="javascript:;">删除</a></td>'
document.getElementById('myBody').appendChild(tr)
}
}
Tr is an object. Instead of writing like this, you might as well just write:
function render(data) {
var html=[]
for (let i = 0; i < data.length; i++) {
let template = '<tr><td>'+data[i].name+'</td><td>'+data[i].age+'</td><td>'+data[i].sex+'</td><td><a href="javascript:;">修改</a> <a href="javascript:;">删除</a></td></tr>'
html.push(template)
document.getElementById('myBody').innerHTML = html.join('')
}
}
Operation effect:
Other features are still being implemented… .
Read More:
- Uncaught TypeError: Failed to execute ‘removeChild’ on ‘Node’: parameter 1 is not of type ‘Node’.
- VUEJS Failed to execute ‘removeChild’ on ‘Node’: The node to be removed is not a child of
- Failed to execute ‘getComputedStyle’ on ‘Window’: parameter 1 is not of type ‘Element’.
- After node.js is installed, use the instruction node version in vscode to show that it is not an external or internal instruction. The solution is as follows:
- How to Fix Module build failed: Error: ENOENT: no such file or directory, scandir xxxxxx\node_…
- How to Fix webpack Module build failed Error: The node API for ‘babel’ has been moved to babel-core
- How to Fix Node.js connection to MYSQL error.
- The Ethereum private chain is built, and the block information cannot be synchronized. The error is resolved: Node data write error err=”state node failed with all peers(1 tries, 1 peers)
- Error: EPERM: operation not permitted, mkdir ‘D:\nodejs\node_modules\npm\node_cache\_npx‘
- Module build failed: Error: ENOENT: no such file or directory, scandir,‘node_modules\node-sass\vend’
- Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded (How to Fix)
- Common error: uncaught typeerror: document.getElementsByClassName (…).addEventListener is not a function
- Error: (22,57) Java: type parameter… Mapper is not in the range of type variable M
- [Solved] jQuery Error: Uncaught TypeError: this.attr is not a function
- How to Solve K8S Error getting node
- How to Fix Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile
- How to Fix Session is not Connecting (How to Diagnose it)
- When C language refers to a user-defined type as a parameter, an error segmentation fault is reported
- Failed to decrypt protected XML node “DTS:Password” with error 0x8009000B “Key not valid for use in …
- Error: ‘default’ is not exported by node_ modules/qs/lib/ index.js