1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| let arr = [ { stuid: 1001, uname: '欧阳霸天', age: 19, gender: '男', salary: '20000', city: '上海' }, { stuid: 1002, uname: '令狐霸天', age: 29, gender: '男', salary: '30000', city: '北京' }, { stuid: 1003, uname: '诸葛霸天', age: 39, gender: '男', salary: '2000', city: '北京' }, ]
let tbody = document.querySelector('tbody') let btn = document.querySelector('.add') let uname = document.querySelector('.uname') let age = document.querySelector('.age') let gender = document.querySelector('.gender') let salary = document.querySelector('.salary') let city = document.querySelector('.city') function fn() { tbody.innerHTML = '' for (let num1 = 0; num1 < arr.length; num1++) { let tr = document.createElement('tr') tr.innerHTML = ` <tr> <td>${arr[num1].stuid}</td> <td>${arr[num1].uname}</td> <td>${arr[num1].age}</td> <td>${arr[num1].gender}</td> <td>${arr[num1].salary}</td> <td>${arr[num1].city}</td> <td><a href="javascript:" id="${num1}">删除</a></td> </tr> ` tbody.appendChild(tr) uname.value = age.value = salary.value = '' gender.value = '男' city.value = '北京' } }
fn()
btn.addEventListener('click', function () {
arr.push({ stuid: arr[arr.length - 1].stuid + 1, uname: uname.value, age: age.value, gender: gender.value, salary: salary.value, city: city.value }) fn() })
tbody.addEventListener('click', function (e) { if (e.target.tagName == 'A') { arr.splice(e.target.id, 1) } fn() })
|