我通过使用appendrow()将新行追加到表的末尾,但是它不起作用,有人可以帮助我吗?

htm:

<body onload="makeTable()">
<table id= "tbl" border="1"></table>
<form>
<input type="button" value=" append a new row " onclick="appendRow()" /><br />
</form>


js:

 function makeTable(){
var theTable =document.getElementById("tbl");
if (theTable.firstChild != null)
     {
      var badIEBody = theTable.childNodes[0];
      theTable.removeChild(badIEBody);
}
var tBody = document.createElement("TBODY");
theTable.appendChild(tBody);

var newRow = document.createElement("tr");
var c1 = document.createElement("td");
var v1 = document.createTextNode("HIT6307");
c1.appendChild(v1);
newRow.appendChild(c1);
var c2 = document.createElement("td");
var v2 = document.createTextNode("Internet Technology");
c2.appendChild(v2);
newRow.appendChild(c2);
tBody.appendChild(newRow);

}

function appendRow() {
  var code = prompt("What is the code of subject", "Type code here");
  var name = prompt("What is the name of subject", "Type name here");

  var tBody = document.getElementByTagName("TBODY");
  newRow = document.createElement("tr");
c1 = document.createElement("td");
v1 = document.createTextNode("code");
c1.appendChild(v1);
newRow.appendChild(c1);
c2 = document.createElement("td");
v2 = document.createTextNode("name");
c2.appendChild(v2);
newRow.appendChild(c2);
tBody.appendChild(newRow);

     tBody.appendChild(newRow);
}



-->
v1 = document.createTextNode(code);


您应该像下面这样写appendRow

function appendRow() {
    var code = prompt("What is the code of subject", "Type code here");
    var name = prompt("What is the name of subject", "Type name here");

    var tBody = document.getElementsByTagName("TBODY")[0];

    newRow = document.createElement("tr");
    c1 = document.createElement("td");
    v1 = document.createTextNode(code);
    c1.appendChild(v1);
    newRow.appendChild(c1);

    c2 = document.createElement("td");
    v2 = document.createTextNode(name);
    c2.appendChild(v2);
    newRow.appendChild(c2);

    tBody.appendChild(newRow);
}


删除行的演示:http://jsfiddle.net/Wqjhj/

关于javascript - javascript中的表格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6236233/

10-13 05:39