< input type = " button " onClick = " add(); " value = " 添加控件 " >
< div id = " a " >
</ div >
< script >
var num = 0 ;
function add() {
alert(window.document.getElementById( " a " ).innerHTML);
window.document.getElementById( " a " ).innerHTML = window.document.getElementById( " a " ).innerHTML + " <br/><input type='text' id='up " + num + " '><a href='#' onClick='javascript:del( " + num + " );'>删除此控件 " + num + " </a> " ;
num = num + 1 ;
}
function del(n) {
var str = window.document.getElementById( " a " ).innerHTML;
var regEx = new RegExp( " <BR><[^<>]*(up " + n + " ){1}[^<>]*><[^<>]*>[^<>]*</A> " );
str = str.replace(regEx, "" );
alert(str);
window.document.getElementById( " a " ).innerHTML = str;
}
</ script >
方法二
//动态添加或删除新的文本框
var num=0;
function add(){
var html="<input type='text' value='' name='poll[]' id='up"+num+"'><a href='#' onClick='javascript:del("+num+");' id='delBtn_"+num+"'>删除此项</a><br/>";
new Insertion.Top('addInput',html);
num=num+1;
}
function del(n){
var node=document.getElementById("up"+n);
var delButton=document.getElementById("delBtn_"+n);
if(node){
node.parentNode.removeChild(node);
delButton.parentNode.removeChild(delButton);
}
}
转自http://btyang.bloghome.cn/posts/85883.html