js创建元素

<!DOCTYPE html>
<html>
<head>
	<title></title>
<script type="text/javascript">
	window.onload=function(){
  var Sub=document.getElementById('sub');
var Name = document.getElementById('username');
var Age= document.getElementById('userage');
   Sub.onclick=function(){
   	var Tab = document.getElementById('tab');
    var x = document.createElement('tr');
    Tab.appendChild(x);
    var x1 = document.createElement('td');
    x1.innerHTML=Name.value;
    Tab.appendChild(x1);
    var x2=document.createElement('td');
    x2.innerHTML=Age.value;
    Tab.appendChild(x2);
	};
};
</script>
</head>
<body>
名字:<input type="text" id="username"><br/>
年龄:<input type="text" id="userage"><br/>
<input type="button" value="提交" id="sub">
<br/><br/><br/><br/><br/>
<table border="2" id="tab">
	<tr>
	  <th>名字</th>
	   <th>年龄</th>
	</tr>
   <tr>
   	<td>tim</td>
   	 	<td>13</td>
   </tr>
</table>
</body>
</html>

在这里插入图片描述

发布了62 篇原创文章 · 获赞 102 · 访问量 3158

猜你喜欢

转载自blog.csdn.net/weixin_44763595/article/details/104823269