博客列表 >js 浏览器中JS、'类'数组、获取DOM操作(Document Object Mode)、表单元素获取、遍历dom树

js 浏览器中JS、'类'数组、获取DOM操作(Document Object Mode)、表单元素获取、遍历dom树

Wu.Ang
Wu.Ang原创
2022年08月22日 12:06:02803浏览

js 浏览器中JS、’类’数组、获取DOM操作、表单元素获取、遍历dom树

浏览器中JS

js使用场景 : 1.传统:浏览器 2.扩展:服务器(Node.js 不涉及DOM)

  1. // 1. 预定义属性,id,class,style
  2. <button id="a" class="b" style="color: red">Btn1</button>
  3. // 2. 自定义属性, data-xxxx
  4. <button data-text="第二个按钮">Btn2</button>
  5. // 3. 预定义的事件属性: on+事件名称, 事件属性的值是JS代码
  6. <button onclick="document.body.style.background='yellow'">Btn3</button>
  7. // 写到事件属性中的js代码,仅限于对当前目标有效,这叫: 内联脚本/行内脚本
  8. <button onclick="setBgc(this)">设置背景色</button>

‘类’数组

  1. const Demo = {
  2. 0 : '冰箱',
  3. 1 : '彩电',
  4. 2 : '洗衣机',
  5. length:3,
  6. };
  7. console.log(Demo);
  8. console.log(Demo[0],Demo[1],Demo[2]);

特征

  1. 类数组,就是一个对象,属性是从0开始递增的正整数,有一个length属性
  2. 类数组与真数组访问方式一样

获取DOM操作(Document Object Mode)

获取模型

  1. 拿到一个 : queerySelector(), 返回一个dom对象
  2. 拿到一组 : querySelectorAll(), 类数组

1.一组元素(NodeList) : querySelectorAll(css-selector)

参数 : css选择器

  1. const items = document.querySelectorAll('.list > .item');
  2. console.log(items);
  3. // NodeList: 节点列表对象
  4. console.log(items.keys());
  5. // Array Iterator {} 只要有这个特征就可以用for-of来遍历
  6. for(let key of items.keys()) console.log(key);
  7. for(let value of items.values()) console.log(value);
  8. // 遍历键值对
  9. for(let ent of items.entries()) console.log(ent);
  10. // forEach 代替for-of
  11. // *************************************************
  12. // forEach(回调函数)
  13. // function (当前正在遍历的值, 当前值的索引/键名) {
  14. // 第二参数(当前值的索引/键名), 是可选的,通常不传
  15. // }
  16. items.forEach((value,key) => {
  17. console.log(key,value);
  18. });
  19. items.forEach(v =>v.style.background = 'red');

2.一个元素 : queerySelector(css-selector)

其实是返回’类数组’中的第一个元素,本质上仍然是在一个集合中查询

  1. const items = document.querySelectorAll('.list > .item:first-of-type');
  2. console.log(items[0]); //返回依然是一个数组
  3. items[0].style.background = 'green';
  4. // 快速获取某个集合中的第一个元素,用于获取唯一元素
  5. const first = document.querySelector('.list > .item');
  6. console.log(first);

3.快捷方式

  1. console.log(document.body);
  2. document.body.style.background='green';
  3. console.log(document.head);
  4. console.log(document.title);

表单元素获取

  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. <link rel="stylesheet" href="css/demo4.css" />
  9. </head>
  10. <body>
  11. <form action="login.php" method="post" id="login">
  12. <fieldset class="login">
  13. <legend>用户登录</legend>
  14. <label for="email">邮箱:</label>
  15. <input type="email" name="email" id="email" value="admin@php.cn" autofocus />
  16. <label for="password">密码:</label>
  17. <input type="password" name="password" id="password" value="123456" />
  18. <button>提交</button>
  19. </fieldset>
  20. </form>
  21. <script>
  22. // 将页面所有的form表单作为数组返回
  23. console.log(document.forms[0]);
  24. console.log(document.forms.login);
  25. console.log(document.forms.login.email.value);
  26. </script>
  27. </body>
  28. </html>

json

  1. // 前后端分离,前端发送json到服务器
  2. // JSON : js对象的序列化/字符串
  3. // 获取表单中的数据
  4. let email = document.forms.login.email.value;
  5. let password = document.forms.login.password.value;
  6. // 转为js对象
  7. let user = {email,password}
  8. let json = JSON.stringify(user);
  9. console.log(json);

遍历dom树

节点类型

  1. window 全局对象
  2. document 文档对象,就是当前的html文档
  3. element 元素对象,<ul><li>
  4. text 文本对象
  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. <link rel="stylesheet" href="css/demo4.css" />
  9. </head>
  10. <body>
  11. <ul class="list">
  12. <li class="item">item1</li>
  13. <li class="item">item2</li>
  14. <li class="item">item3</li>
  15. <li class="item">item4</li>
  16. <li class="item">item5</li>
  17. <li class="item">item6</li>
  18. </ul>
  19. <script>
  20. let ul = document.querySelector('.list');
  21. // 获取ul下元素的节点类型(包括文本,空格,换行)
  22. console.log(ul.childNodes);
  23. // 只获取元素节点
  24. console.log(ul.children); //类数组
  25. // 将类数组转为真数组
  26. // 1.Array包装
  27. console.log(Array.from(ul.children));
  28. // 2. [...rest]
  29. console.log([...ul.children]);
  30. </script>
  31. </body>
  32. </html>

子元素

  1. // 获取第一个元素
  2. // [...ul.children][0] == ul.firstElementChild
  3. ul.firstElementChild.style.color='red';
  4. // 最后一个
  5. // [...ul.children][ul.children.length-1].style.color = 'red';
  6. ul.lastElementChild.style.color = 'blue';

兄弟元素

  1. // 后一个
  2. ul.firstElementChild.nextElementSibling.style.color='green';
  3. // 前一个
  4. ul.lastElementChild.previousElementSibling.style.color = 'pink';

父元素

  1. // 父节点 : 一定是元素节点或文档节点
  2. ul.firstElementChild.parentElement.style.background = '#333';
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议