javascript动态添加删除文本框


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
var count = 0;
function Add(){
   count += 1;
   var File1 = document.getElementById("file1");
   var div = document.createElement("div");
   var countTxt = document.createTextNode("Hardware"+count);
   var inputTxt = document.createElement("input");
   inputTxt.type = "text";
   inputTxt.name = "txt"+count;
   var btn = document.createElement("input");
   btn.type = "button";
   btn.value = "delete";
   btn.onclick = function(){
       this.parentNode.parentNode.removeChild(this.parentNode);
       var str = File1.innerHTML;
       var re = /[^<]+/i;
       var n = File1.getElementsByTagName("div");
       for(var k=0;k<n.length;k++){
           n[k].innerHTML = n[k].innerHTML.replace(re,"Hardware"+(k+1));
           n[k].getElementsByTagName("input").item(1).onclick = this.onclick;
       }
       count -= 1;
   }
   
   div.appendChild(countTxt);
   div.appendChild(inputTxt);
   div.appendChild(btn);
   File1.appendChild(div);
}
</script>
</head>
<body>
<input type="button" name="button" value="add" onclick="Add();" />
<div id="file1"></div>
</body>
</html>

你可能感兴趣的:(JavaScript,java,html)