动态添加表单节点

以添加附件为例,点击添加附件按钮,下方将添加一个能添加附件的组件,点击他右边的删除按钮,将删除该附件。

<!DOCTYPE html>
<html>
  <head>
    <title>addFile.html</title>
    <meta charset=UTF-8>
	<script type="text/javascript">
	
		//添加节点
		function addFile(){
			var oTab = document.getElementById("tab");
			var oTr = document.createElement("tr");
			oTr.innerHTML="<td><input type='file'/></td><td><button onclick='delFile(this)'>删除</btton></td>"
			oTab.appendChild(oTr);
		}
		
		//删除节点
		function delFile(node){
			var oTrNode = node.parentNode.parentNode;
			oTrNode.parentNode.removeChild(oTrNode);
		}
	</script>
  </head>
  
  <body>
  	<table id="tab">
  		<tr><td>
  			<button onclick="addFile()">添加附件</button>
  		</td></tr>
  		<!-- 点击添加附件按钮,在这里添加tr -->
  	</table>
  </body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_38238041/article/details/80983107