动态添加删除文本框
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <meta name="generator" content="editplus" /> <meta name="author" content="Gao YiXiang" /> <meta name="email" content="yixianggao@126.com" /> <meta name="keywords" content="javascript dhtml dom" /> <meta name="description" content="I love web development." /> </head> <body> <script type="text/javascript"> var count = 0; function Add(num){ count += 1; var addNewDiv = document.getElementById("addNewDiv"); var div = document.createElement("div"); var countTxt = document.createTextNode("文本框"+count); var inputTxt = document.createElement("input"); var tipspan = document.createElement("span"); tipspan.id = "tip"+count; tipspan.innerHTML = "这个元素已经存在"; tipspan.style.display = 'none'; inputTxt.type = "text"; inputTxt.name = "test"; inputTxt.id = count; var btn = document.createElement("input"); btn.type = "button"; btn.value = "删除"; inputTxt.onblur = function(){ test(this.id,"tip"+this.id); }; inputTxt.onfocus = function(){ document.getElementById("tip"+this.id).style.display = "none"; } btn.onclick = function() { this.parentNode.parentNode.removeChild(this.parentNode); var n = addNewDiv.getElementsByTagName("div"); for(var k=0; k<n.length; k++) { n[k].firstChild.nodeValue = "文本框"+(k+1); } count -= 1; } div.appendChild(countTxt); div.appendChild(inputTxt); div.appendChild(btn); div.appendChild(tipspan); addNewDiv.appendChild(div); } function test(id,tipdivid){ var test = new Array(); test = document.getElementsByName('test'); var testvalue = document.getElementById(id).value; for(var i=0;i<test.length;i++){ if(test[i].id != id && test[i].value == testvalue && test[i].value !=''){ document.getElementById(tipdivid).style.display = "inline-block"; } } } </script> <div id="addNewDiv"> </div> <input name="xiyaoname" value="Add" type="button" onclick="Add();" /> </body> </html>