1 回答
TA贡献1802条经验 获得超5个赞
您需要用于addEventListener添加任何事件。例子 :button.addEventListener('click', purchase)
var app = document.getElementById("app");
var table = document.createElement('table');
app.appendChild(table)
for (var i = 1; i <= 5; i++) {
var th = document.createElement('th')
//th.appendChild(header)
table.appendChild(th)
for (var j = 1; j <= 4; j++) {
var tr = document.createElement('tr')
for (var k = 1; k <= 5; k++) {
var td = document.createElement('td')
var node = document.createTextNode(k)
td.appendChild(node)
tr.appendChild(td)
if (k === 5) {
var button = document.createElement('input')
button.setAttribute('type', 'submit')
button.setAttribute('value', 'Purchase')
button.addEventListener('click', purchase)
td.appendChild(button)
}
}
table.appendChild(tr)
}
}
function purchase() {
console.log("done")
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id='app'>
</div>
添加回答
举报