如何使用javascript删除表上的spesific行?使用document.createElement()时
这是我的密码如何使用javascript删除表上的spesific行?使用document.createElement()时,javascript,html,Javascript,Html,这是我的密码 function AddItemOnTable(){ if(getCookie("no") == null){ var no = 1; }else if(parseInt(getCookie("no")) > 0){ var no = getCookie("no"); }else{ var no = 1; } var tableRef = d
function AddItemOnTable(){
if(getCookie("no") == null){
var no = 1;
}else if(parseInt(getCookie("no")) > 0){
var no = getCookie("no");
}else{
var no = 1;
}
var tableRef =
document.getElementById('whatToBuy').getElementsByTagName('tbody')[0];
var newRow = tableRef.insertRow(tableRef.rows.length);// Nambahin row di
tabel diurutan terakhir
var cell_no = newRow.insertCell(0);// Tambah row pada index yang ke 0
var newText = document.createTextNode(String(no));// Memberikan text
cell_no.appendChild(newText);
no = String(parseInt(no) + 1);
document.cookie = "no="+no;
var cell_btn = newRow.insertCell(7);
var input = document.createElement("input");
input.type = "button";
input.className = "button";
input.value = "x";
var index = parseInt(no-2);
//alert(index);
input.onclick = "DeleteRow(index)";
cell_btn.appendChild(input);
}
function DeleteRow(no){
document.getElementById("whatToBuy").deleteRow(no);
alert("a");
}
在:input.onclick=DeleteRowindex
为什么它不调用删除行的功能?
很抱歉我的英语不好,我是新来的网站开发人员:D发布的代码有两个问题 首先:button元素的onclick属性需要定义为一个函数对象 设置onclick=doSomething可以在HTML中用于在元素的开始标记内设置单击处理程序,但只能由HTML解析器解析,不能在纯JavaScript中使用 第二:deleteRow方法使用表中行的当前从零开始的索引,但正在传递一个参数,该参数是从cookie或原始表位置获得的no值 此处建议的解决方案是对所有行使用相同的DeleteRow函数,但要对其进行修改以查找所单击按钮的当前行位置: 变量编号=100;//测试 var table=document.getElementByIdwhatToBuy; 函数addRows{//测试 对于变量i=0;i<5;++i{ var button=document.createElementBUTTON; button.type=按钮; button.textContent=删除索引+否; button.onclick=DeleteRow; var row=document.createElementTR; var cell=document.createElementTD; cell.appendChildbutton; 行。子单元格; 表1.2子行; ++没有; } } 函数DeleteRow{ //查找要删除的行; 对于var row=this;row=row.parentNode{ 如果row.tagName==TR{ 打破 } } var rows=table.querySelectorAllTR; 对于变量i=0;i