动态生成table

<!DOCTYPE HTML>
<html>
 <head>
  <title>动态生成table</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <style type="text/css">
  body{font-size:12px;font-family:"微软雅黑";}
  #btn{background:#333;width:80px;height:25px;line-height:25px;color:#fff;text-align:center;display:inline-block;text-decoration:none;border-radius:5px;}
  table{border:1px solid #666;}
  td{border:1px solid #222;padding:10px;cursor:pointer;}
  #color{width:30px;height:30px;margin:0 5px;display:inlineblock;}
  #num{color:#f60;margin:0 5px;font-size:15px;}-
  #his{font-size:14px;font-weight:500;}
  </style>
 </head>


 <body>
  <div>
  <label>行数<input type="text" value="" id="row"></label><label>列数<input type="text" value="" id="col"></label><a href="javascript:void(0)" id="btn">生成表格</a>
  </div>
  <div id='b_s' style="display:none">你所选择的区域是<span id="num"></span>,颜色为<span id="color"></span><em id="his"></em></div>
  <div id="ta"></div>
  <script type="text/javascript">
  function $(obj)
  {
  return document.getElementById(obj);
  }
  window.οnlοad=function(){
  var _row=$("row");
  var _col=$("col");
  var _btn=$("btn");
  _btn.οnclick=function(){
  var _ta=$("ta");
  _ta.innerHTML='';
  $("b_s").style.display="none";
  if(_row.value == "")
  {
  alert("输入下会死啊");
  _row.focus();
  }
  else if(isNaN(_row.value))
  {
  alert("非数字的");
  _row.select();
  }
  else if(_col.value =="")
  {
  alert("输00000");
  _col.focus();
  }
  else if(isNaN(_col.value))
  {
  alert("非数字的飘过");
  _col.select();
  }
  else
  {
  var _rowF=parseInt(_row.value);
  var _colF=parseInt(_col.value);
  var _table=document.createElement("table");
  for(var i=0;i<_rowF;i++)
  {
  var _tr=document.createElement("tr");
  for(var n=0;n<_colF;n++)
  {
  var _td=document.createElement("td");
  _td.innerHTML=Math.floor(Math.random() * 10);
  _tr.appendChild(_td);
  _td.style.backgroundColor="#"+BgColor();


  }
  _table.appendChild(_tr);


  }
  _ta.appendChild(_table);
  _row.value="";
  _col.value="";
  tdClick();
  }
  }
  function BgColor()//随机背景
  {
  var bg=Math.ceil(Math.random() * 12651548).toString(16);
  if(bg.length < 6)
  {
  "0"+ bg
  }
  return  bg
  }
  function tdClick(){
  var _tds=$("ta").getElementsByTagName("td");
  console.log(_tds.length+"33")
for(var i=0;i<_tds.length;i++){
_tds[i].οnclick=function(){
console.log("s")
$("b_s").style.display="block";
$("num").innerHTML=this.innerHTML;
$("color").style.backgroundColor=this.style.backgroundColor;
$("his").innerHTML=this.style.backgroundColor;
}
}
}
  }
  </script>
 </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值