Blogger Information
Blog 18
fans 0
comment 2
visits 10452
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js的自定义dataset对象 ,js操作css, js中事件的添加与删除
go0
Original
909 people have browsed it

用户自定义属性

主要用于js脚本控制,data-为前缀

  1. <body>
  2. <div class="btn-group">
  3. <button data-index="1" onclick="getIndex(this)">btn1</button>
  4. <button data-index="2" onclick="getIndex(this)">btn2</button>
  5. <button data-index="3" onclick="getIndex(this)">btn3</button>
  6. </div>
  7. </body>

onclick是预定义属性
data-index是自定义属性

看清上图中,不要加“data-”


再看一个例子


注意,“price-lowest”转成驼峰写法“priceLowest”,否则中间的哪个短横会被当做减号。干脆就别用短横,用下划线。

dataset可读可写。

  1. xiGua.dataset.priceLowest = 1.3;
  2. console.log(xiGua.dataset.priceLowest);

js操作css

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. <style>
  9. div {
  10. width: 150px;
  11. height: 100px;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div style="color: red; background-color: lightcyan">hello</div>
  17. <script>
  18. const div = document.querySelector("div");
  19. console.log(div.style.color);
  20. console.log(div.style.height); // 用style只能拿到行内样式,拿不到width和height
  21. console.log(window.getComputedStyle(div).width);
  22. console.log(window.getComputedStyle(div).backgroundColor);
  23. const width = window.getComputedStyle(div).width;
  24. console.log(width, typeof width); // 拿到的width是个string
  25. console.log(typeof parseInt(width)); // 拿到的width是个string
  26. div.style.width = parseInt(width) * 2 + "px"; // 这样宽度就扩大了2倍
  27. </script>
  28. </body>
  29. </html>


js操作css的class属性

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. <style>
  9. .red {
  10. color: red;
  11. }
  12. .bgc {
  13. background-color: yellow;
  14. }
  15. .blue {
  16. color: blue;
  17. }
  18. .bd {
  19. border: 5px solid black;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <h2>hello</h2>
  25. <script>
  26. const h2 = document.querySelector("h2");
  27. // h2.className = "red bgc"; // 这样的方式可以
  28. // 建议下面这种用法
  29. h2.classList.add("red");
  30. h2.classList.add("bgc");
  31. // contains用于判断
  32. console.log(h2.classList.contains("bgc")); // 返回true
  33. // remove用于移除
  34. h2.classList.remove("bgc");
  35. //replace用于替换
  36. h2.classList.replace("red", "blue"); // 第一个参数是被替换的
  37. // toggle用于切换
  38. h2.classList.toggle("bd");
  39. h2.classList.toggle("bd");
  40. </script>
  41. </body>
  42. </html>

js中事件的添加与删除

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <button>元素对象1</button>
  11. <button>元素事件监听器</button>
  12. <button>事件派发</button>
  13. <script>
  14. const btn1 = document.querySelector("button:first-of-type");
  15. btn1.onclick = function () {
  16. console.log(event);
  17. };
  18. btn1.onclick = null; // 删除事件
  19. const btn2 = document.querySelector("button:nth-of-type(2)");
  20. function show() {
  21. console.log(event);
  22. }
  23. btn2.addEventListener("click", show); //添加事件
  24. btn2.removeEventListener("click", show); // 移除事件
  25. const btn3 = document.querySelector("button:nth-of-type(3)");
  26. // 先添加一个事件,然后自动触发他
  27. let i = 0;
  28. btn3.addEventListener(
  29. "click",
  30. () => {
  31. console.log("恭喜,又赚了:" + i + "元");
  32. i += 0.8;
  33. },
  34. false
  35. );
  36. //创建一个自定义事件
  37. const myclick = new Event("click");
  38. btn3.dispatchEvent(myclick);
  39. btn3.dispatchEvent(myclick);
  40. btn3.dispatchEvent(myclick);
  41. btn3.dispatchEvent(myclick);
  42. //一次性定时器
  43. setTimeout(() => btn3.dispatchEvent(myclick), 2000);
  44. //连续定时器
  45. setInterval(() => btn3.dispatchEvent(myclick), 1000);
  46. </script>
  47. </body>
  48. </html>
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