-
Notifications
You must be signed in to change notification settings - Fork 8
/
15-6.html
395 lines (391 loc) · 11.1 KB
/
15-6.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>15-6</title>
<style>
html,ul{
margin: 0;
padding: 0;
}
body,fieldset,ul{
padding: 15px;
}
body{
margin: 50px;
}
fieldset{
margin: 50px;
}
button{
cursor: pointer;
}
.box{
width: 100px;
height: 100px;
margin: 25px;
border: 1px solid #000;
background-color: salmon;
}
.box1.active{
background-color: orange;
}
.get-attr,.set-attr,.get-wh,.set-wh{
margin-bottom: 15px;
padding: 15px;
}
.box10{
padding: 20px;
border: 5px solid #000;
}
.box11{
width: 200px;
background-color: pink;
position: fixed;
top: 50px;
right: 50px;
}
</style>
<script src="js/jquery-3.2.1.js"></script>
</head>
<body>
<fieldset>
<legend>1、给元素$node添加class及删除其class</legend>
<div class="box box1"></div>
<button class="btn1">改变box颜色</button>
<button class="btn2">恢复box颜色</button>
<script>
$(".btn1").on("click",function(){
$(".box1").addClass("active");
});
$(".btn2").on("click",function(){
$(".box1").removeClass("active");
});
</script>
</fieldset>
<fieldset>
<legend>2、展示元素$node, 隐藏元素$node</legend>
<div class="box box2"></div>
<button class="btn3">隐藏 box</button>
<button class="btn4">展示 box</button>
<button class="btn5">toggle</button>
<script>
$(".btn3").on("click",function(){
$(".box2").hide();
});
$(".btn4").on("click",function(){
$(".box2").show();
});
$(".btn5").on("click",function(){
$(".box2").toggle();
});
</script>
</fieldset>
<fieldset>
<legend>3、获取元素$node的属性: id、src、title, 修改以上属性</legend>
<img src="https://jirengu.com/addons/theme/stv1/_static/app/index-new/imgs/2.gif" alt="photo" id="pic3" title="jirengu">
<div class="get-attr">
<button class="btn6">得到img属性</button>
</div>
<div class="set-attr">
<input type="text" placeholder="please enter new id" class="set-id">
<input type="text" placeholder="please enter new src" class="set-src">
<input type="text" placeholder="please enter new title" class="set-title">
<button class="btn7">设置img属性</button>
</div>
<script>
var $id = $("#pic3").attr("id"),
$src = $("#pic3").attr("src"),
$title = $("#pic3").attr("title");
var imgAttr = "";
imgAttr += "<p>当前img的id为:" + $id + "</p>";
imgAttr += "<p>当前img的src为:" + $src + "</p>";
imgAttr += "<p>当前img的title为:" + $title + "</p>";
$(".btn6").on("click",function(){
$(".get-attr").append(imgAttr);
});
$(".btn7").on("click",function(){
$("img").attr("id",$(".set-id").val());
$("img").attr("src",$(".set-src").val());
$("img").attr("title",$(".set-title").val());
var $newId = $("img").attr("id"),
$newSrc = $("img").attr("src"),
$newTitle = $("img").attr("title");
var newImgAttr = "";
newImgAttr += "<p>当前img的id为:" + $newId + "</p>";
newImgAttr += "<p>当前img的src为:" + $newSrc + "</p>";
newImgAttr += "<p>当前img的title为:" + $newTitle + "</p>";
$(".set-attr").append(newImgAttr);
});
</script>
</fieldset>
<fieldset>
<legend>4、给$node添加自定义属性data-src</legend>
<img src="https://jirengu.com/addons/theme/stv1/_static/app/index-new/imgs/3.gif" alt="pp" data-src="https://jirengu.com/addons/theme/stv1/_static/app/index-new/imgs/4.gif" id="pic4">
<div>
<button class="btn8">通过data-src来更换照片</button>
</div>
<script>
$(".btn8").on("click",function(){
$("#pic4").attr("src",$("#pic4").attr("data-src"));
});
</script>
</fieldset>
<fieldset>
<legend>5、在$ct内部最开头添加元素$node</legend>
<ul class="ct5">
<li>1</li>
<li>2</li>
</ul>
<button class="btn9">在$ct内部最开头添加元素</button>
<script>
$(".btn9").on("click",function(){
$(".ct5").prepend("<li>3</li>");
});
</script>
</fieldset>
<fieldset>
<legend>6、在$ct内部末尾添加元素$node</legend>
<ul class="ct6">
<li>1</li>
<li>2</li>
</ul>
<button class="btn10">在$ct内部末尾添加元素</button>
<script>
$(".btn10").on("click",function(){
$(".ct6").append("<li>3</li>");
});
</script>
</fieldset>
<fieldset>
<legend>7、删除$node</legend>
<ul class="ct7">
<li>0</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
</ul>
<button class="btn11">删除li</button>
<script>
$(".btn11").on("click",function(){
$(".ct7 li").remove(":odd");
});
</script>
</fieldset>
<fieldset>
<legend>8、把$ct里内容清空</legend>
<ul class="ct8">
<li>0</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
</ul>
<button class="btn12">清空$ct</button>
<script>
$(".btn12").on("click",function(){
$(".ct8").empty();
});
</script>
</fieldset>
<fieldset>
<legend>9、在$ct里设置html</legend>
<div class="ct9">这里将会有新内容</div>
<button class="btn13">在$ct添加html</button>
<script>
$(".btn13").on("click",function(){
$(".ct9").html("<p>jirengu.com</p>");
});
</script>
</fieldset>
<fieldset>
<legend>10、获取、设置$node的宽度、高度(分别不包括内边距、包括内边距、包括边框、包括外边距)</legend>
<div class="box box10"></div>
<div class="get-wh">
<button class="btn14">获取box宽高</button>
</div>
<div class="set-wh">
<input type="text" placeholder="设置新的宽度" class="set-w">
<input type="text" placeholder="设置新的高度" class="set-h">
<button class="btn15">设置box宽高</button>
</div>
<script>
$(".btn14").on("click",function(){
var $box10 = $(".box10"),
$width = $box10.width(),
$height = $box10.height(),
$innerWidth = $box10.innerWidth(),
$innerHeight = $box10.innerHeight(),
$outerWidth = $box10.outerWidth(),
$outerHeight = $box10.outerHeight(),
$outerWidths = $box10.outerWidth(true),
$outerHeights = $box10.outerHeight(true);
$(".get-wh").append("<p>当前元素的宽度为:" + $width + "px</p>");
$(".get-wh").append("<p>当前元素的宽度(包括内边距)为:" + $innerWidth + "px</p>");
$(".get-wh").append("<p>当前元素的宽度(包括边框)为:" + $outerWidth + "px</p>");
$(".get-wh").append("<p>当前元素的宽度(包括外边距)为:" + $outerWidths + "px</p>");
$(".get-wh").append("<p>当前元素的高度为:" + $height + "px</p>");
$(".get-wh").append("<p>当前元素的高度(包括内边距)为:" + $innerHeight + "px</p>");
$(".get-wh").append("<p>当前元素的高度(包括边框)为:" + $outerHeight + "px</p>");
$(".get-wh").append("<p>当前元素的高度(包括外边距)为:" + $outerHeights + "px</p>");
});
$(".btn15").on("click",function(){
var $setW = $(".set-w").val(),
$setH = $(".set-h").val();
$(".box10").width($setW);
$(".box10").height($setH);
});
</script>
</fieldset>
<fieldset>
<legend>11、获取窗口滚动条垂直滚动距离</legend>
<p>在右上角!!!</p>
<div class="box box11"></div>
<script>
$(window).on("scroll",function(){
$(".box11").html("滚动条距离窗口顶部的距离是:" + $(window).scrollTop() + "px");
});
</script>
</fieldset>
<fieldset>
<legend>12、获取$node到根节点水平、垂直偏移距离</legend>
<div class="box12 box"></div>
<div class="get-offset">
<button class="btn16">获取offset</button>
</div>
<script>
$(".btn16").on("click",function(){
var $offsetX = $(".box12").offset().left,
$offsetY = $(".box12").offset().top;
$(".get-offset").append("<p>该box到根节点的水平距离为:" + $offsetX + "px</p>");
$(".get-offset").append("<p>该box到根节点的垂直距离为:" + $offsetY + "px</p>");
});
</script>
</fieldset>
<fieldset>
<legend>13、修改$node的样式,字体颜色设置红色,字体大小设置14px</legend>
<p class="p13">jirengu hunger hello</p>
<button class="btn17">修改样式</button>
<script>
$(".btn17").on("click",function(){
$(".p13").css({
"color": "red",
"font-size": "14px"
});
});
</script>
</fieldset>
<fieldset>
<legend>14、遍历节点,把每个节点里面的文本内容重复一遍</legend>
<ul class="ct14">
<li>html</li>
<li>css</li>
<li>javascript</li>
<li>jquery</li>
</ul>
<div class="get-text">
<button class="btn18">遍历节点得到text</button>
</div>
<script>
$(".btn18").on("click",function(){
$(".ct14 li").each(function(){
$(".get-text").append("<p>" + $(this).text() + "</p>");
});
});
</script>
</fieldset>
<fieldset>
<legend>15、从$ct里查找class为 .item的子元素</legend>
<ul class="ct15">
<li>html</li>
<li>css</li>
<li>js</li>
<li class="item">.item</li>
<li>jquery</li>
</ul>
<button class="btn19">将.item颜色变为红色</button>
<script>
$(".btn19").on("click",function(){
$(".ct15").find("li.item").css("color","red");
});
</script>
</fieldset>
<fieldset>
<legend>16、获取$ct里面的所有孩子</legend>
<ul class="ct16">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<button class="btn20">将$ct所有的孩子设置为红色</button>
<script>
$(".btn20").on("click",function(){
$(".ct16").children().css("color","red");
});
</script>
</fieldset>
<fieldset>
<legend>17、对于$node,向上找到class为.ct的父亲,在从该父亲找到.panel的孩子</legend>
<div class="ct">
<ul>
<li>1</li>
<li>2</li>
<li class="node">node</li>
<li>3</li>
<li>4</li>
</ul>
<div class="panel">panel</div>
</div>
<p>点击node会改变panel颜色</p>
<script>
$(".node").on("click",function(){
$(".node").parents(".ct").find(".panel").css("color","red");
});
</script>
</fieldset>
<fieldset>
<legend>18、获取选择元素的数量</legend>
<ul class="ct18">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<div class="get-len">
<button class="btn21">获取.ct子元素的数量</button>
</div>
<script>
$(".btn21").on("click",function(){
var $len = $(".ct18 li").length;
$(".get-len").append("<p>.ct子元素的数量为:" + $len + "</p>")
});
</script>
</fieldset>
<fieldset>
<legend>19、获取当前元素在兄弟中的排行</legend>
<ul class="ct19">
<li>0</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<p class="ranking">当前元素的排行为:</p>
<script>
$(".ct19 li").on("mouseenter",function(){
var $index = $(this).index();
$(".ranking").text("当前元素的排行为:" + $index);
});
</script>
</fieldset>
</body>
</html>