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>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
生成动态弹窗可以使用JavaScript和HTML结合的方式来实现。首先,我们需要在HTML中添加一个弹窗的容器,例如: ```html <div class="modal"> <div class="modal-content"> <span class="close">×</span> <p>这是一个动态弹窗!</p> </div> </div> ``` 其中,`modal`是整个弹窗的容器,`modal-content`是弹窗的内容区域,`close`用于关闭弹窗。 接下来,我们可以使用JavaScript来动态生成弹窗,并添加相应的样式和交互事件,示例代码如下: ```javascript // 获取弹窗容器 var modal = document.querySelector('.modal'); // 获取关闭按钮 var closeBtn = document.querySelector('.close'); // 当用户点击关闭按钮时,隐藏弹窗 closeBtn.onclick = function() { modal.style.display = 'none'; } // 当用户点击页面上的某个按钮时,显示弹窗 var openBtn = document.querySelector('.open'); openBtn.onclick = function() { modal.style.display = 'block'; } ``` 在上述代码中,我们首先获取了弹窗容器和关闭按钮的DOM元素,然后分别为关闭按钮和页面上的某个按钮添加了点击事件,当用户点击关闭按钮时,我们通过修改`modal`元素的`display`属性来隐藏弹窗,当用户点击页面上的某个按钮时,我们同样通过修改`modal`元素的`display`属性来显示弹窗。 最后,我们可以使用CSS样式来美化弹窗的样式,例如: ```css /* 弹窗容器样式 */ .modal { display: none; position: fixed; z-index: 1; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgba(0,0,0,0.4); } /* 弹窗内容样式 */ .modal-content { background-color: #fefefe; margin: 15% auto; padding: 20px; border: 1px solid #888; width: 80%; } /* 关闭按钮样式 */ .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: black; text-decoration: none; cursor: pointer; } ``` 通过以上代码,我们就可以实现一个简单的动态弹窗。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr顺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值