javascript中的表
我正在使用appendrow()将新行追加到表的末尾,但它不起作用,有人能帮我吗 htm:javascript中的表,javascript,html,Javascript,Html,我正在使用appendrow()将新行追加到表的末尾,但它不起作用,有人能帮我吗 htm: javascript中没有函数getElementByTagName,只有getElementsByTagName`(元素列表) 此外,您正在使用字符串常量(“code”&“name”)填充新的文本元素,而不是相应变量的值: v1=document.createTextNode(“代码”) 您应该按照以下方式编写appendRow: function appendRow() { var code
javascript中没有函数
getElementByTagName
,只有getElementsByTagName`
(元素列表)
此外,您正在使用字符串常量(“code”&“name”)填充新的文本元素,而不是相应变量的值:
v1=document.createTextNode(“代码”)代码>
您应该按照以下方式编写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);
}
行删除演示:console/jslint怎么说???@DrStrangeLove它的“document.getElementByTagName不是函数”try document.getElementsByTagname您应该删除createTextNode(“code”)中的引号代码>&createTextNode(“名称”)代码>谢谢,它很有效。插入表格后,我可以选择并删除此行吗?我创建了一个使用代码执行此操作的示例(经过一些修改)——删除是通过单击按钮完成的。查看我的答案,最后是一个指向JSFIDLE的链接。@maniji我在单击按钮时尝试删除一行,但如何在此处选择一行?
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);
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);
}
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);
tBody.appendChild(newRow);
}