转自: http://www.javaeye.com/problems/2639
- <html>
- <head>
- <title>动态添加html元素</title>
- <script type="text/javascript">
- <!--
- var textNumber = 1;
- function addCheckDetail(form,afterElement){
- textNumber++;
-
- var label2=document.createElement("label");
- label2.appendChild(document.createTextNode("问题所属方面:"));
- var select=document.createElement("select");
- select.setAttribute("select","select"+textNumber);
- select.setAttribute("size","1");
- select.setAttribute("id","select"+textNumber);
- var option1=document.createElement("option");
- option1.setAttribute("value","1");
- option1.appendChild(document.createTextNode("方面一"));
- var option2=document.createElement("option");
- option2.setAttribute("value","2");
- option2.appendChild(document.createTextNode("方面二"));
- label2.appendChild(select);
- select.appendChild(option1);
- select.appendChild(option2);
- form.insertBefore(label2,afterElement);
-
-
- var label1 = document.createElement("label");
-
- var textField = document.createElement("textarea");
- textField.setAttribute("name","txt"+textNumber);
- textField.setAttribute("cols",80);
- textField.setAttribute("rows",3);
- textField.setAttribute("id","txt"+textNumber);
-
- label1.appendChild(document.createTextNode("问题描述"+textNumber+":"));
-
- label1.appendChild(textField);
-
- form.insertBefore(label1,afterElement);
- }
- function removeCheckDetail(form,afterElement){
-
- if (textNumber > 1) {
-
- form.removeChild(document.getElementById("select"+textNumber).parentNode);
- form.removeChild(document.getElementById("txt"+textNumber).parentNode);
- textNumber--;
- }
- }
-
- </script>
- <style type="text/css">
- <!--
- label {
- display:block;
- margin:.25em 0em;
- }
- -->
- </style>
- </head>
- <body>
- <form id="myForm" method="get" action="./" />
<html>
<head>
<title>动态添加html元素</title>
<script type="text/javascript">
<!--
var textNumber = 1;
function addCheckDetail(form,afterElement){
textNumber++;
//创建列表标签
var label2=document.createElement("label");
label2.appendChild(document.createTextNode("问题所属方面:"));
var select=document.createElement("select");
select.setAttribute("select","select"+textNumber);
select.setAttribute("size","1");
select.setAttribute("id","select"+textNumber);
var option1=document.createElement("option");
option1.setAttribute("value","1");
option1.appendChild(document.createTextNode("方面一"));
var option2=document.createElement("option");
option2.setAttribute("value","2");
option2.appendChild(document.createTextNode("方面二"));
label2.appendChild(select);
select.appendChild(option1);
select.appendChild(option2);
form.insertBefore(label2,afterElement);
// 创建文本标签
var label1 = document.createElement("label");
// 创建文本框
var textField = document.createElement("textarea");
textField.setAttribute("name","txt"+textNumber);
textField.setAttribute("cols",80);
textField.setAttribute("rows",3);
textField.setAttribute("id","txt"+textNumber);
// 增加标签文本注释
label1.appendChild(document.createTextNode("问题描述"+textNumber+":"));
// 把textField放入标签中
label1.appendChild(textField);
// 把所有的这些增加到form中
form.insertBefore(label1,afterElement);
}
function removeCheckDetail(form,afterElement){
// 假如有文本框个数超过一个
if (textNumber > 1) {
// 删除最后一个添加的文本框
form.removeChild(document.getElementById("select"+textNumber).parentNode);
form.removeChild(document.getElementById("txt"+textNumber).parentNode);
textNumber--;
}
}
//-->
</script>
<style type="text/css">
<!--
label {
display:block;
margin:.25em 0em;
}
-->
</style>
</head>
<body>
<form id="myForm" method="get" action="./" />
- <table><tbody>
- <label>问题所属方面:
- <select name="select" size="1" id="">
- <option value="1">问题一</option>
- <option value="2">问题二</option>
- </select>
- </label>
- <label>问题描述1:<textarea name="txt1" cols="80" rows="3"></textarea></label>
-
- <p>
- <input type="button" value="添加一个问题" onclick="addCheckDetail(this.form,this.parentNode)" />
- <input type="button" value="删除最后一个问题" onclick="removeCheckDetail(this.form)" />
- </p>
- <p><input type="Submit" value="保存" /></p>
<table><tbody>
<label>问题所属方面:
<select name="select" size="1" id="">
<option value="1">问题一</option>
<option value="2">问题二</option>
</select>
</label>
<label>问题描述1:<textarea name="txt1" cols="80" rows="3"></textarea></label>
<p>
<input type="button" value="添加一个问题" onclick="addCheckDetail(this.form,this.parentNode)" />
<input type="button" value="删除最后一个问题" onclick="removeCheckDetail(this.form)" />
</p>
<p><input type="Submit" value="保存" /></p>
- </tbody></table>
-
- </form>
- </body>
- </html>
</tbody></table>
</form>
</body>
</html>
我想用javascript动态增加行,当form中没有table标签时是可以增加的,像上面增加了table标签就出现错误了,请问各位该如何解决?
=======================================
解决方案:
由于form.insertBefore是将指定节点添加到form的直接子节点上,由于form中只有一个table,没有afterElement这个直接子节点.所以会报错.
input标签的parentNode即p标签,它的直接父结点是TBODY标签.所以只能用TBodyElement.insertBefore.进行插入.
把
- form.insertBefore(label2,afterElement);
- form.insertBefore(label1,afterElement);
form.insertBefore(label2,afterElement);
form.insertBefore(label1,afterElement);
两句改为:
- afterElement.parentNode.insertBefore(label2,afterElement);
- afterElement.parentNode.insertBefore(label1,afterElement);
afterElement.parentNode.insertBefore(label2,afterElement);
afterElement.parentNode.insertBefore(label1,afterElement);
posted on 2008-12-15 23:23
CopyHoo 阅读(805)
评论(0) 编辑 收藏 所属分类:
Java Web