JS如何生成动态列表

编程学习 2021-07-04 14:06www.dzhlxh.cn编程入门
这篇文章主要为大家详细介绍了JS如何生成动态列表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了JS如何生成动态列表的具体代码,供大家参考,具体内容如下

思路: JS中写函数----HTML中利用表单元素获取用户输入的行和列的值,调用JS中的函数。
主要用到的知识有:JS就不说了,表单元素,以及其属性value,和表格…

代码:

<!doctype html>
<html>
<head>
 <meta charset = "utf-8">
 <title>创建动态表格</title>
 <style>
  #form1{
   padding:10px; width:400px; margin:0 auto;
  }
 </style>
 <script type = "text/javascript">
  function createTable() { 
   var r1 = document.getElementById( "row" ).value;
   var c1 = document.getElementById( "col" ).value; 
   var n = 1;  //单元格中的文字
   var str = "<table width = '100%' border = '1' cellspacing = '0' cellpadding = '0' ><tbody>";
   for(i = 0; i<r1; i++) {
    str = str+"<tr align = 'center'>"
    for(j = 0; j<c1; j++) str = str+"<td>"+(n++)+"</td>"
    str = str+"</tr>"
   }
   var d1 = document.getElementById( "d1" );
   d1.innerHTML = str+"</tbody></table>";
  }
 </script>
</head>
<body>
 <form id = "form1" name = "form1" method = "post" action = "">
  <fieldset>
  <legend>动态创建表格</legend>
  输入表格的行数:<input type = "text" id = "row" placeholder = "请输入表格的行数" required autofocus /><br/>
  输入表格的列数:<input type = "text" id = "col" placeholder = "请输入表格的列数" /><br/>
  <input type = "button" id = "ok" value = "产生表格" onclick = "createTable()"/>
  </fieldset>
 </form>
 <div id = "d1"></div>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板