首页 > 其他分享 >JS-ClassList and ClassName的用法

JS-ClassList and ClassName的用法

时间:2022-11-04 15:35:42浏览次数:38  
标签:color ClassList 元素 100px JS ClassName div 类名

ClassList:

1.ClassList属性有以下几种方法:

(1)通过ClassList可以获取元素的全部类名;

(2)通过ClassList.remove()可以删除元素的一个或多个类名;

(3)通过ClassList.add()可以向元素添加一个或多个类名;

(4)通过ClassList.lentgh可以获取到元素类名的数量;

(5)通过ClassList.item(x)可以获取到元素类名索引为x的类名;

(6)通过ClassList.toggle可以为元素切换类;

(7)通过ClassList.contains(x)查看元素是否存在类名为"x"的类;
 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .one {
        width: 100px;
        height: 100px;
        background-color: red;
      }
      .bbc {
        width: 100px;
        height: 100px;
        background-color: black;
      }
    </style>
  </head>
  <body>
    <div class="one">1234556</div>
    <script>
      var div = document.querySelector(".one");
      div.classList.remove("one");
      div.classLIst.toggle(".bbc");
    </script>
  </body>
</html>

 

className 属性,设置或返回元素的 class 属性

 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .name {
      width: 100px;
      height: 100px;
      background-color: red;
    }
  </style>
  <body>
    <div></div>
    <script>
      var div = document.querySelector("div");
      div.className = "name";
    </script>
  </body>
</html>

 

标签:color,ClassList,元素,100px,JS,ClassName,div,类名
From: https://www.cnblogs.com/ZhuAo/p/16857954.html

相关文章

  • JSONArray数据值作为条件查询时--in
    背景:需要查询一段业务并且是以JSONArray中的值作为判断条件之一的:json_contains(字段名,json_array(值),'$') 数据库字段:  SQL写法:   lambdaQuery写法:......
  • json 工具类
    json工具类/**LicensedtotheApacheSoftwareFoundation(ASF)underoneormore*contributorlicenseagreements.SeetheNOTICEfiledistributedwith......
  • Next.js Environment Variables All In One
    Next.jsEnvironmentVariablesAllInOne.env.env.localDB_HOST=localhostDB_USER=myuserDB_PASS=mypassword//getStaticProps://pages/index.jsexportas......
  • leetcode 160. 相交链表 js 实现
    给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。图示两个链表在节点 c1 开始相交: ......
  • JS 探索 this 指向问题
    1.扑朔迷离的this从表面来看,this总能通过各种变通的方式得到意想不到的结果既然是“意想不到”,就说明对“各种变通的方式”不太了解2.那么我们来看看“各种变......
  • js 密码强度检测工具
    效果:  新建一个密码强度检测工具文件 password-strength.js://hasnumberconsthasNumber=(number)=>newRegExp(/[0-9]/).test(number);//hasmixofs......
  • download.js (实例3)下载
     downloadFile(url,"电子作业票APP.jpg");//fileRealPath文件全路径//fileName文件名带格式的exportfunctiondownloadFile(fileRealPath,fileName){l......
  • JS书写规范
    JS变量命名命名方法小驼峰式命名命名规范前缀应当是名词。(函数的名字前缀为动词,以此区分变量和函数)命名建议尽量在变量名字中体现所属类型,如:length、count等表示......
  • download.js (实例2)下载
    调用this.$download.url('/api/v1/org/question/exportData',datVal,`${this.questMess.name}.xlsx`);js文件import{saveAs}from'file-saver'importaxi......
  • el-dialog 或dialog 封装 通过js控制
    1.通用组件封装dialogImg.vue<template><div><el-dialogv-show="isShow":visible.sync="isShow"width="50%"@close="seePicUrl='......