zoukankan      html  css  js  c++  java
  • classList用法

    <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>classList</title>
            <style type="text/css">
                .box{
                    width: 100px;
                    height: 100px;
                    margin: 10px 0;
                    background-color: blue;
                }
                .red{
                    background-color: red;
                }
            </style>
        </head>
        <body>
            <div class="box"></div>
            <div class="box"></div>
            <script type="text/javascript">
          /*classList:获取class列表属性
            length class的长度
            add() 添加class方法
            remove() 删除class方法
            toggle() 切换class方法
          */
    var box = document.querySelectorAll('.box'); /*for (var i = 0; i < box.length; i++) { box[i].isChecked = false; box[i].onclick = function () { if (this.isChecked) { this.className = 'box'; } else { this.className = 'box red'; } this.isChecked = !this.isChecked; } }*/ /*for (var i = 0; i < box.length; i++) { box[i].isChecked = false; box[i].onclick = function () { console.dir(this); if (this.isChecked) { this.classList.remove('red'); } else { this.classList.add('red'); } this.isChecked = !this.isChecked; } }*/ // 三目写法 for (var i = 0; i < box.length; i++) { box[i].isChecked = false; box[i].onclick = function () { console.dir(this); /*if (this.isChecked) { this.classList.remove('red'); } else { this.classList.add('red'); }*/ // this.isChecked ? this.classList.remove('red') : this.classList.add('red'); this.classList[this.isChecked?'remove':'add']('red'); this.isChecked = !this.isChecked; } } </script> </body> </html>
  • 相关阅读:
    STM32 --- 什么时候打开复用IO的时钟(比如RCC_APB2Periph_AFIO)
    STM32 一直进入串口接收中断
    printf 中的 %.*s
    形参定义为二级指针,可以修改实参指针本身的值
    结构体和联合体配合使用
    自定义注解的实现思路
    log4j application.properties 配置文件
    外观设计模式
    适配器设计模式
    模版方法设计模式
  • 原文地址:https://www.cnblogs.com/jianglibaizhi/p/10573432.html
Copyright © 2011-2022 走看看