如何在JavaScript中添加和删除动态控件?
方法一:
<inputtype="button"onClick="add();"value="添加控件">
<divid="a">
</div>
<script>
varnum=0;
functionadd(){
alert(window.document.getElementById("a").innerHTML);
window.document.getElementById("a").innerHTML=window.document.getElementById("a").innerHTML+"<br/><inputtype='text'id='up"+num+"'><ahref='#'onClick='javascript:del("+num+");'>删除此控件"+num+"</a>";
num=num+1;
}
functiondel(n){
varstr=window.document.getElementById("a").innerHTML;
varregEx=newRegExp("<BR><[^<>]*(up"+n+"){1}[^<>]*><[^<>]*>[^<>]*</A>");
str=str.replace(regEx,"");
alert(str);
window.document.getElementById("a").innerHTML=str;
}
</script>
<divid="a">
</div>
<script>
varnum=0;
functionadd(){
alert(window.document.getElementById("a").innerHTML);
window.document.getElementById("a").innerHTML=window.document.getElementById("a").innerHTML+"<br/><inputtype='text'id='up"+num+"'><ahref='#'onClick='javascript:del("+num+");'>删除此控件"+num+"</a>";
num=num+1;
}
functiondel(n){
varstr=window.document.getElementById("a").innerHTML;
varregEx=newRegExp("<BR><[^<>]*(up"+n+"){1}[^<>]*><[^<>]*>[^<>]*</A>");
str=str.replace(regEx,"");
alert(str);
window.document.getElementById("a").innerHTML=str;
}
</script>
本文地址:http://www.45fan.com/a/question/67595.html