javascript14--classlist

classlist

<style>
    body,dl,dd,p,h1,h2,h3,h4,h5,h6{margin:0;}
    ol,ul,li{margin:0;padding:0;list-style:none;}
    img{border:none;vertical-align:top;}
    .box{
      width:100px ;
      height: 100px;
      border: 1px solid #000;
    }
   .box.on{
     background-color: #f60;
    }
  </style>
</head>
<body>
  <div class="box"></div>
</body>
<script>
  var box = document.getElementsByClassName("box")[0];
  box.cname = box[0].className;
  box.onmouseenter = function () {
      this.className = this.cname+" on";//注意加空格
  }
  box.onmouseleave = function () {
      this.className = this.cname;
  }
</script>

但如果默认是on状态 <div class="box on"></div>并且忘了之前取的名字

每个元素身上都有一个Domtokenlist(标记列表)是一个类数组,存储着该元素的所有类名 并且索引--值的形式储存

  1. for循环
  2. 删/修改/查找/增加
  3. 我们不能直接修改classlist和增加classlist当中的类名 box.classList[2]="lisi"这样并不会增加第三个类名
  4. 判断有没有类名 ele.classList.containts("类名") 返回值是false 或者trueconsole.log(box.classList.contains("lisi"));
  5. 获取类名 ele.classList.item(0) 传索引值 console.log(box.classList.item(0));相当于ele.classList[0]
  6. 增加类名 ele.classList.add("类名") 增加类名 从后面加
  7. 删除类名 ele.classList.remove
  8. 切换类名 ele.classList.toggle("类名") 有则改之 无责加勉 如果有类名 则删除 如果没有类名则加上
  9. 只知道索引 怎么删呢? ele.classList.remove(ele.classList.item(0));
  10. 如果加类名一定要记住一次只能加 不能一次加多个 box.classList.add("lisi" "wangwu")

math

  1. math 是js内置的一个对象 是包含数学方法的一个对象
  2. console.log(Math.random());打印会发现是个大于0 的小数 刷新后 数字会变化Math.random()返回一个[0,1) 能取到0,永远取不到1 一个16位小数的伪随机数Math.random()括号里面不接受传参
  3. 如果要取5-10 console.log(Math.random()*5+5);
  4. Math.floor() 向下取整 舍去小数 取整数 console.log(Math.floor(5.11));结果是5
  5. Math.ceil 向上取整 不管小数有多小 向上取整 console.log(Math.ceil(5.00011));结果是6
  6. Math.trunc()舍去小数 当都是正数时 和floor效果一样 但是当是负数时候console.log(Math.floor(-5.00011)); 结果是-6 console.log(Math.trunc(-5.00011));结果是-5
  7. Math.round()四舍五入
  8. Math.cos()
  9. Math.sin()
  10. Math.tan()
  11. Math.abs()绝对值
  12. Math.max() 取最大值
  13. Math.min()
  14. Math.sqrt()平方根
  15. Math.cbrt()立方根
  16. Math.pI()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值