欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取表单元素,dom树的遍历与常用属性

程序员文章站 2022-03-01 18:34:21
...

获取表单元素

console.log(document.forms[0]);
form.id
console.log(document.forms.login);
input:name
console.log(document.forms.login.email);
value
console.log(document.forms.login.email.value);

DOM遍历

第一个firstElementChild
ul.firstElementChild.style.backgroundColor = "yellow";
下一个nextElementSibling
ul.firstElementChild.nextElementSibling.style.backgroundColor = "green";
前一个previousElementSibling
ul.lastElementChild.previousElementSibling.style.backgroundColor = "lightgreen";
最后一个lastElementChild
ul.lastElementChild.style.backgroundColor = "yellow";
父节点parentElement
ul.lastElementChild.parentElement.style.border = "4px solid";
元素节点parentNode
ul.lastElementChild.parentNode.style.border = "4px solid red";

dom的增删改

增加元素 append

  1. for (let i = 0; i < 5; i++) {
  2. const li = document.createElement("li");
  3. li.textContent = "item" + (i + 1);
  4. ul.append(li);
  5. }

移除 remove
ul.lastElementChild.remove();
替换 replaceChild

  1. const last = document.querySelector("ul li:last-of-type");
  2. const a = document.createElement("a");
  3. a.textContent = "https://php.cn";
  4. ul.replaceChild(a, last);

js操作元素内容常用方法

textContent:返回元素以及后代元素中的所有内容,包括style,display:none的内容
innerText:返回元素以及后代中的文本
innerHTML:返回内部的html中的字符串
outerHTML:返回当前节点的自身的html字符串
textContent首选,innerText,2016年才成为w3c标准
innerHTML可解析html字符,innerText:不解析html 字符

留言板

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>dom实战:留言板</title>
  8. </head>
  9. <body>
  10. <input type="text" onkeydown="addMsg(this)" placeholder="请输入留言内容" autofocus />
  11. <ul class="list"></ul>
  12. <script>
  13. function addMsg(ele) {
  14. console.log(ele);
  15. console.log(event);
  16. console.log(event.key);
  17. if (event.key === "Enter") {
  18. const ul = document.querySelector(".list");
  19. if (ele.value.length === 0) {
  20. alert("留言板内容不为空");
  21. ele.focus();
  22. return false;
  23. }
  24. const li = document.createElement("li");
  25. li.textContent = ele.value;
  26. ul.append(li);
  27. ul.insertAdjacentElement("afterbegin", li);
  28. ul.lastElementChild.style.backgroundColor = "yellow";
  29. ele.value = null;
  30. ele.focus();
  31. }
  32. }
  33. </script>
  34. </body>
  35. </html>

dataset对象

dataset 可读可写

  1. function getIndex(btn) {
  2. console.log("点击了第", btn.dataset.index, "个按钮");
  3. }
  4. const user = document.querySelector(".user");
  5. console.log(user.dataset.email);
  6. console.log(user.dataset.workUnit);
  7. user.dataset.workUnit = "php.cn";
  8. console.log(user.dataset.workUnit);

获取元素的计算样式

getComputedStyle

  1. const div = document.querySelector("div");
  2. console.log(window.getComputedStyle(div).width);
  3. console.log(window.getComputedStyle(div).height);

classlist对象常用方法

1.传统方式

  1. const h2 = document.querySelector("h2");
  2. h2.className = "red bgc";

2.classList add添加

  1. h2.classList.add("red");
  2. h2.classList.add("bgc");

3.contains;判断
console.log(h2.classList.contains("bgc"));
4.remove 移除class
h2.classList.remove("bgc");
5.replace 替换
h2.classList.replace("red", "blue");
6.toggle:切换class
h2.classList.toggle("bd");

事件的添加与派发

1.事件添加

  1. const btn1 = document.querySelector("button:first-of-type");
  2. btn1.onclick = function () {
  3. console.log(event);
  4. };

事件派发

  1. const btn3 = document.querySelector("button:nth-of-type(3)");
  2. let i = 0;
  3. btn3.addEventListener(
  4. "click",
  5. () => {
  6. console.log("恭喜你,又赚了" + i + "元");
  7. i += 0.8;
  8. },
  9. false
  10. );

推荐阅读