1 回答
TA贡献1831条经验 获得超10个赞
// 给你写个demo吧
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
</style>
<body>
<table id="ItemContainer" border="1" width="100"></table>
<script>
function add (name) {
alert(name);
}
function update() {
var container = document.getElementById("ItemContainer");
console.log(container);
container.innerHTML = "";
for(let i=0; i<5; i++) {
let name = i;
let tr = document.createElement('tr');
let td = document.createElement('td');
td.innerHTML = i;
tr.appendChild(td);
console.log(name)
tr.onclick = function (){
return add(name);
};
container.appendChild(tr);
}
}
update()
</script>
</body>
</html>
添加回答
举报