博客列表 >循环函数DOM简例

循环函数DOM简例

牡丹飞
牡丹飞原创
2020年11月04日 14:51:31744浏览

1 效果

2 代码

  1. function BodyAddP(doing = "do while", num = 0) {
  2. document.body.appendChild(document.createElement("p")).innerHTML = "我来报个数:" + doing + "->" + num;
  3. }
  4. SetBox1BackColor = () => (document.querySelector(".box").className = "box green");
  5. SetBox1BackColor();
  6. SetBoxBackColor = (color = "green", ...boxs) => {
  7. let tempColor = "";
  8. for (box of boxs) {
  9. tempColor = "box " + color;
  10. document.querySelector(".box:nth-of-type(" + box + ")").className = tempColor;
  11. }
  12. };
  13. function RowAddP(context = "row p") {
  14. document.getElementsByName("selector")[0].innerHTML += "<p>" + context + "</p>";
  15. }
  16. SetBoxBackColor("green", 4, 6, 9);
  17. SetBoxBackColor("sienna", ...[2, 3, 4]);
  18. Selector = () => {
  19. RowAddP("tagname->" + document.getElementsByTagName("h1")[0].innerHTML);
  20. RowAddP("name->" + document.getElementsByName("byname")[0].innerHTML);
  21. RowAddP("id->" + document.getElementById("love3").innerHTML);
  22. RowAddP("className->" + document.getElementsByClassName("boxLove")[3].innerHTML);
  23. RowAddP("querySelectorAll->" + document.querySelectorAll(".boxLove")[4].innerHTML);
  24. let selectorArr = document.querySelectorAll(".boxLove:nth-of-type(n+5)");
  25. console.log(selectorArr);
  26. for (v of selectorArr) {
  27. document.querySelectorAll(".boxLove")[5].innerHTML += "<p>" + v.innerHTML + "</p>";
  28. }
  29. document.querySelectorAll(".boxLove")[7].classList.toggle("boxLoveWidth");
  30. document.querySelectorAll(".boxLoveWidth")[0].innerHTML +=
  31. "<p>" +
  32. "data-ok->" +
  33. document.querySelector("#data-test").dataset.ok +
  34. "</p>" +
  35. "<p>" +
  36. "data-hello-ok->" +
  37. document.querySelector("#data-test").dataset.helloOk +
  38. "</p>";
  39. document.querySelector("#data-test").dataset.ok = 10000;
  40. document.querySelectorAll(".boxLoveWidth")[0].innerHTML +=
  41. "<p> set" +
  42. "data-ok->" +
  43. document.querySelector("#data-test").dataset.ok +
  44. "</p>" +
  45. "<p>" +
  46. "data-hello-ok->" +
  47. document.querySelector("#data-test").dataset.helloOk +
  48. "</p>";
  49. };
  50. Selector();
  51. BodyAddP(0);
  52. let i = 1;
  53. let arrGoodHe = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  54. do {
  55. if (i <= 10) {
  56. BodyAddP("do while", i);
  57. i++;
  58. }
  59. while (i > 10 && i < 21) {
  60. BodyAddP("while", i);
  61. i++;
  62. }
  63. if (i > 20 && i < 31) {
  64. for (j = i; j < 31; j++) {
  65. BodyAddP("for", i);
  66. i++;
  67. }
  68. }
  69. if (i > 30 && i < 41) {
  70. for (v in arrGoodHe) {
  71. BodyAddP("for in", parseInt(v) + i);
  72. }
  73. i += 10;
  74. }
  75. if (i > 40 && i < 51) {
  76. arrGoodHe.forEach(function (v, index) {
  77. BodyAddP("forEach", parseInt(index) + i);
  78. });
  79. i += 10;
  80. }
  81. if (i > 50 && i <= 100) {
  82. for (let k = 0; k < 5; k++) {
  83. for (v of arrGoodHe) {
  84. BodyAddP("for of", 10 * k + parseInt(v) + i - 1);
  85. }
  86. }
  87. i += 50;
  88. }
  89. } while (i <= 100);

3 总结

感谢老师教导!
主要学习for、function、DOM访问。
手还是有些生熟,没有按时做作业。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议