[英]Why onclick() didn't work inside the button tag?
I put the onclick()
inside the button
tag but it's give me an SyntaxError
. 我将
onclick()
放在button
标记内,但这给了我SyntaxError
。
I want to delete every rows if I click the button
in the right corner of every rows. 如果我单击每行右上角的
button
,我想删除每行。
var firstName = document.getElementById("txtFName");
var lastName = document.getElementById("txtLName");
var sex = document.getElementById("selSex");
var age = document.getElementById("txtAge");
var indice = [];
var id = 1;
var remove = "Remove";
var tblPerson = document.getElementById("tblPerson");
function remove(indice) { //remove row one by one
for(var i = 0; i < indice.length; i++)
myArray.splice(indice, 1);
}
function print() {
var objPerson = new Person(id, firstName.value, lastName.value, selSex.value, age.value, remove); //instantiation
var display = "";
display += "<tr>";
display += "<td>" + objPerson._id + "</td>";
display += "<td>" + objPerson._firstName + "</td>";
display += "<td>" + objPerson._lastName + "</td>";
display += "<td>" + objPerson._sex + "</td>";
display += "<td>" + objPerson._age + "</td>";
display += "<td>" + "<button onclick = "remove()">" + objPerson._remove + "</button>" +
"</td>"; //onclick gives me a SyntaxError
display += "</tr>";
tblPerson.innerHTML += display; //display the output many times
id++; //increment id
}
Because you have a concatenation error in this line 因为这行有串联错误
display += "<td>" + "<button onclick = "remove()">" + objPerson._remove + "</button>"
which should be 应该是
display += "<td>" + "<button onclick = 'remove()'>" + objPerson._remove + "</button>"
or 要么
display += "<td>" + "<button onclick = \"remove()\">" + objPerson._remove + "</button>"
Except I seriously advise against concatenating html strings because it's not elegant and flexible at all. 除非我认真建议不要串联html字符串,因为它根本不优雅且不灵活。 Use document.createElement() instead.
请改用document.createElement()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.