欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js实现华丽的九九乘法表效果

程序员文章站 2023-11-16 15:48:34
效果图: 代码如下:

效果图:

js实现华丽的九九乘法表效果

代码如下:

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>span实现的乘法表</title>
 <style type="text/css">
  .wrap {
   width: 800px;
   margin: 20px auto;
   -webkit-user-select: none;
   -moz-user-select: none;
   -ms-user-select: none;
   user-select: none;
  }

  span {
   padding: 3px;
   font-weight: 700;
   display: inline-block;
   line-height: 30px;
   border-bottom: 2px solid #000000;
   border-left: 2px solid #000000;
   width: 80px;
   cursor: pointer;
  }
  .last {
   margin-top: -2px;
   border-top: 2px solid #000000;
   border-right: 2px solid #000000;
  }

  .bg0 {
   background-color: yellow;
  }

  .bg1 {
   background-color: lawngreen;
  }

  .bg2 {
   background-color: lightblue;
  }

  .hover {
   background-color: #bb3bd9;
  }
  .only{
   background-color: lightsalmon;
  }
 </style>
</head>
<body>
<script>
 function multiplication(a) {
  var str = '<div class="wrap">';
  for (var i = 1; i <= a; i++) {
   for (var j = 1; j <= i; j++) {
    var curstr = j + ' x ' + i + '= ' + i * j;
    j !== i ? str += '<span class="' + 'bg' + (i % 3) + '">' + curstr + '</span>' : str += '<span class="' + 'last bg' + (i % 3) + '">' + curstr + '</span><br/>';
   }
  }
  str += "</div>";
  document.write(str);
 }
 i

 multiplication(9);

 var ospans = document.getelementsbytagname("span");
 for (var i = 0; i < ospans.length; i++) {
  ospans[i].onclick = function () {
   //alert("我是第 "+parseint(this.innerhtml)+" 行");
   alert(this.innerhtml + " 我在第 " + this.innerhtml.charat(4) + " 行");
  }
  ospans[i].onmouseover = function () {
   //console.log(111);
   var num = this.innerhtml.charat(4);
   //console.log(num);
   for (var j = 0; j < ospans.length; j++) {
    if (ospans[j].innerhtml.charat(4) === num) {
     ospans[j].classlist.add("hover");
    }
   }
   this.classlist.add("only");
  }
  ospans[i].onmouseout = function () {
   var num = this.innerhtml.charat(4);
   for (var j = 0; j < ospans.length; j++) {
    if (ospans[j].innerhtml.charat(4) === num) {
     ospans[j].classlist.remove("hover");
    }
   }
   this.classlist.remove("only");
  }
 }
</script>
</body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!