Blogger Information
Blog 9
fans 0
comment 0
visits 4006
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
访问器属性原理及获取DOM元素方法
Original
416 people have browsed it

属性访问器,其实是一个函数、一个方法,是伪装成属性的方法,使可以在对象中像属性一样的访问方法

  1. user = {
  2. data:{
  3. name:'jack',
  4. age:20,
  5. },
  6. get age(){
  7. return this.data.age;
  8. },
  9. set age(age){
  10. this.data.age = age;
  11. },
  12. }
  13. console.log(user.age);
  14. user.age =30;
  15. console.log(user.age);

DOM中获取元素的方法
获取一组元素使用querySelectorAll(),获取单个元素使用querySelector()
```html
<!DOCTYPE html>

<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<ul>
<li class="item">item1</li>
<li class="item">item2</li>
<li class="item">item3</li>
<li class="item">item4</li>
<li class="item">item5</li>
</ul>
<script>
/ 获取类名为’item’的所有元素 /
console.log(document.querySelectorAll(‘.item’));
/ 获取类名为’item’的第一个元素 /
console.log(document.querySelector(‘.item’));
/ 获取body元素 /
console.log(document.body);
/ 获取head元素 /
console.log(document.head);
/ 获取title元素 /
console.log(document.title);
/ 获取html元素 /
console.log(document.documentElement);

  1. </script>

</body>
</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