-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.html
179 lines (165 loc) · 5.19 KB
/
dom.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
margin: 0;
padding: 0;
}
body {
height: 800px;
}
.box-outer {
position: relative;
width: 180px;
height: 200px;
border: brown 2px solid;
overflow: scroll;
}
.box {
box-sizing: border-box;
width: 80px;
height: 300px;
margin-left: 12px;
margin-top: 14px;
padding-left: 16px;
padding-top: 15px;
}
#boxId {
background-color: brown;
}
.bg {
background-color: bisque;
}
.span-box {
position: relative;
margin-top: 20px;
margin-left: 30px;
border: springgreen 2px solid;
width: 80px;
height: 100px;
}
#span-1 {
color: rebeccapurple;
}
#span-2 {
color: royalblue;
}
</style>
</head>
<body>
<h1 class="bg">前端小课:公众号素燕</h1>
<div class="box-outer">
<div id="boxId" class="box">红色的箱子</div>
</div>
<p class="bg">修改颜色</p>
<div class="span-box">
<span id="span-1">我是第一个span</span>
<span id="span-2">我是第二个span</span>
</div>
<script>
let queryById = function (id) {
let elm = document.getElementById(id);
// elm.style.backgroundColor = 'red';
};
queryById('boxId');
</script>
<script>
let querySelectorId = function (id) {
let elm = document.querySelector(id);
console.log(id, '-------------------------');
console.log('getClientRects', elm.getClientRects());
console.log('getBoundingClientRect', elm.getBoundingClientRect());
let log = function (elm) {
for (key in elm) {
let whiteList = [
'offsetTop', 'offsetLeft', 'offsetWidth', 'offsetHeight',
'scrollTop', 'scrollLeft', 'scrollWidth', 'scrollHeight',
'clientTop', 'clientLeft', 'clientWidth', 'clientHeight',
'offsetParent'
];
if (whiteList.indexOf(key) !== -1) {
console.log(key, ' = ', elm[key]);
}
}
}
log(elm);
elm.onscroll = function () {
log(elm);
}
};
window.onscroll = function () {
querySelectorId('#boxId');
querySelectorId('.box-outer');
};
// querySelectorId('#boxId');
// querySelectorId('#span-1');
// querySelectorId('#span-2');
</script>
<script>
let querySelector = function (id) {
let elm = document.querySelector(id);
for (key in elm) {
let isEvent = false;
if (key.indexOf('on') !== -1) {
isEvent = true;
}
let isInBlackList = false;
if (key.indexOf('DOCUMENT') !== -1) {
isInBlackList = true;
}
if (key.indexOf('ELEMENT') !== -1) {
isInBlackList = true;
}
if (key.indexOf('COMMENT') !== -1) {
isInBlackList = true;
}
if (key.indexOf('ATTRIBUTE') !== -1) {
isInBlackList = true;
}
if (key.indexOf('ENTITY') !== -1) {
isInBlackList = true;
}
if (key.indexOf('CDATA') !== -1) {
isInBlackList = true;
}
if (typeof elm[key] !== 'function' && !isEvent && !isInBlackList) {
console.log(key, ' = ', elm[key]);
}
}
};
// querySelector('p.bg');
</script>
<script>
let querySelectorAll = function (selector) {
let elms = document.querySelector(selector);
// elms.forEach(item => {
// console.log(item.constructor);
// console.log(item);
// });
// console.log(elms);
};
// 通过 ID 选择器 #boxId 查找
querySelectorAll('#boxId');
// 通过类选择器 .bg 查找
querySelectorAll('.bg');
// 通过类选择器 .box 查找
querySelectorAll('.box');
// 通过标签名查找
querySelectorAll('p');
querySelectorAll('div');
// 查找标签名为 h1,选择器为 .bg 的元素
querySelectorAll('h1.bg');
// 可通过逗号分割,查询多个选择器
querySelectorAll('.bg, .box');
</script>
<!-- <h1 class="bg">前端小课:公众号素燕</h1>
<div id="boxId" class="box"></div>
<p class="bg">修改颜色</p>
<img src="./listen.png" width="100px" alt="听"> -->
</body>
</body>
</html>