请教高手,在页面中有一个"添加按钮"每点击一次在页面上添加一个文本框和一个"删除" 按钮,添加的文本框的名字都一样,点击"删除"按钮,删除所对应的文本框,同时该删除按钮也一并删除,求原代码
------解决方案--------------------
- HTML code
<!DOCTYPE HTML>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
</style>
</head>
<body>
<button id="add">添加</button>
<script>
function $(el){
return typeof el == 'string' ? document.getElementById(el) : el;
}
function $t(name, cot){
cot = cot || document;
return cot.getElementsByTagName(name);
}
$('add').onclick = function(){
var p = document.createElement('p');
p.innerHTML = '<input name="test" /> <button>删除</button>';
$t('button', p)[0].onclick = function(){
this.parentNode.parentNode.removeChild( this.parentNode );
}
document.body.appendChild(p);
}
</script>
</body>
</html>