Blogger Information
Blog 47
fans 0
comment 3
visits 44714
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
dataset与classlist的使用案例
江流
Original
492 people have browsed it
  • html最初状态
    ```html

    <!-- 自定义属性:sno -->

    <div class="stu bgc hw" data-sno="0001">学生信息</div>

  1. - css
  2. ```css
  3. .bgc {
  4. background-color: seagreen;
  5. }
  6. .bgc2 {
  7. background-color: slateblue;
  8. }
  9. .fgc1 {
  10. color: seashell;
  11. }
  12. .hw {
  13. height: 20rem;
  14. width: 30rem;
  15. }
  • 效果

  • 写入javascript

  1. const stu = document.querySelector(".stu");
  2. // 将自定义属性sno的值写入div内容
  3. stu.textContent += stu.dataset.sno;
  4. // 添加类
  5. stu.classList.add("fgc1");
  6. // 使用类bgc2替换类bgc
  7. stu.classList.replace("bgc", "bgc2");
  8. // 删除类hw
  9. stu.classList.remove("hw");
  • 效果
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post