-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.html
435 lines (271 loc) · 14.7 KB
/
1.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<!DOCTYPE HTML>
<html lang="en-US" >
<head>
<meta charset="UTF-8">
<title>微博小案例 | 主要内容</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 1.0.3">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="./2.html" />
<link rel="prev" href="./index.html" />
</head>
<body>
<link rel="stylesheet" href="gitbook/style.css">
<div class="book" data-level="0.1" data-basepath="." data-revision="1494503212468">
<div class="book-summary">
<div class="book-search">
<input type="text" placeholder="Type to search" class="form-control" />
</div>
<ul class="summary">
<li class="chapter " data-level="0" data-path="index.html">
<a href="./index.html">
<i class="fa fa-check"></i>
day03
</a>
<ul class="articles">
<li class="chapter active" data-level="0.1" data-path="1.html">
<a href="./1.html">
<i class="fa fa-check"></i>
<b>0.1.</b>
微博小案例
</a>
</li>
<li class="chapter " data-level="0.2" data-path="2.html">
<a href="./2.html">
<i class="fa fa-check"></i>
<b>0.2.</b>
cookie存储
</a>
</li>
<li class="chapter " data-level="0.3" data-path="3.html">
<a href="./3.html">
<i class="fa fa-check"></i>
<b>0.3.</b>
sessionStorage存储
</a>
</li>
<li class="chapter " data-level="0.4" data-path="5.html">
<a href="./5.html">
<i class="fa fa-check"></i>
<b>0.4.</b>
localStorage存储
</a>
</li>
<li class="chapter " data-level="0.5" data-path="6.html">
<a href="./6.html">
<i class="fa fa-check"></i>
<b>0.5.</b>
应用程序缓存
</a>
</li>
<li class="chapter " data-level="0.6" data-path="06.html">
<a href="./06.html">
<i class="fa fa-check"></i>
<b>0.6.</b>
hash简单介绍
</a>
</li>
<li class="chapter " data-level="0.7" data-path="07.html">
<a href="./07.html">
<i class="fa fa-check"></i>
<b>0.7.</b>
实现跨域网络访问
</a>
</li>
<li class="chapter " data-level="0.8" data-path="08.html">
<a href="./08.html">
<i class="fa fa-check"></i>
<b>0.8.</b>
js模板简单说明
</a>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="http://www.gitbook.io/" target="blank" class="gitbook-link">Published using GitBook</a>
</li>
</ul>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header">
<!-- Actions Left -->
<a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a>
<a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a>
<div id="font-settings-wrapper" class="dropdown pull-left">
<a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i>
</a>
<div class="dropdown-menu font-settings">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="buttons">
<button type="button" id="reduce-font-size" class="button size-2">A</button>
<button type="button" id="enlarge-font-size" class="button size-2">A</button>
</div>
<div class="buttons font-family-list">
<button type="button" data-font="0" class="button">Serif</button>
<button type="button" data-font="1" class="button">Sans</button>
</div>
<div class="buttons color-theme-list">
<button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
<button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
<button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
</div>
</div>
</div>
<!-- Actions Right -->
<div class="dropdown pull-right">
<a href="#" class="btn toggle-dropdown" aria-label="Toggle share dropdown"><i class="fa fa-share-alt"></i>
</a>
<div class="dropdown-menu font-settings dropdown-left">
<div class="dropdown-caret">
<span class="caret-outer"></span>
<span class="caret-inner"></span>
</div>
<div class="buttons">
<button type="button" data-sharing="twitter" class="button">Twitter</button>
<button type="button" data-sharing="google-plus" class="button">Google</button>
<button type="button" data-sharing="facebook" class="button">Facebook</button>
<button type="button" data-sharing="weibo" class="button">Weibo</button>
<button type="button" data-sharing="instapaper" class="button">Instapaper</button>
</div>
</div>
</div>
<a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Share on Google Plus"><i class="fa fa-google-plus"></i></a>
<a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Share on Facebook"><i class="fa fa-facebook"></i></a>
<a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Share on Twitter"><i class="fa fa-twitter"></i></a>
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="./" >主要内容</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1">
<div class="page-inner">
<section class="normal" id="section-gitbook_3395">
<h1 id="">微博小案例</h1>
<p><strong>01 实现点赞和踩的功能 </strong></p>
<p>实现思路:</p>
<pre><code>(1)要实现点赞和踩的功能,需要给点赞,踩等按钮添加点击事件
(2)在写入微博内容之后,先找到对应的标签(通过div.find("class"));
(3)抽取方法addTopClickEvent|addDownClickEvent等,把查找到的标签作为参数传递给该方法
(4)说明顶和踩等操作都需要发送网络请求,演示终极版本的页面说明
(5)查看对应的接口,说明参数中用到了id属性,那么封装好的方法中就需要多一个参数(obj对象)
(6)发送点赞的网络请求,说明请求成功之后服务器返回的结果是{error:0},没有意义,我们需要做的就是请求成功之后+1
(7)同样的思路处理踩的功能(直接拷贝修改请求路径)
</code></pre><p>核心代码:</p>
<pre><code> function addTopClickEvent(dom,obj) {
//添加点击事件
dom.click(function () {
//发送网络请求
$.ajax({
"url":baseURL,
"data":"act=acc&id="+obj.id,
"type":"GET",
"success":function (response) {
dom.text(parseInt(dom.text()) + 1);
}});
});
}
function addDownClickEvent(dom,obj) {
//添加点击事件
dom.click(function () {
//发送网络请求
$.ajax({
"url":baseURL,
"data":"act=ref&id="+obj.id,
"type":"GET",
"success":function (response) {
dom.text(parseInt(dom.text()) + 1);
}});
});
}
</code></pre><p><strong>02 实现删除操作</strong></p>
<p>实现思路:</p>
<pre><code>(1)参考已经实现的点赞和踩功能,封装addDelClickEvent方法,该方法接收两个参数jQuery的实例对象和该对象对应的obj对象(存放微博数据的id)
(2)删除操作:说明dom.remove方法无法直接删除自己
使用dom.parents(".replay").remove()方法可以删除,但是删除完微博数据之后发现还是需要更新获取最新的数据
(3)删除操作:重新获取默认的微博数据 + 重新获取页码信息
</code></pre><p>核心代码:</p>
<pre><code> function addDelClickEvent(dom,obj) {
//添加点击事件
dom.click(function () {
//发送网络请求
$.ajax({
"url":baseURL,
"data":"act=ref&id="+obj.id,
"type":"GET",
"success":function (response) {
//dom.text(parseInt(dom.text()) + 1);
//找到父节点然后删除
//dom.parents(".reply").remove();
//方案二:重新请求获取当前页面信息
getMsgWithPage(1);
getPageCount();
}});
});
}
</code></pre><p> <strong>03 使用模板引擎进行处理 </strong></p>
<p> 地址:<a href="https://github.com/aui/art-template" target="_blank">https://github.com/aui/art-template</a></p>
<p> 使用步骤:</p>
<pre><code> (1) 引入框架的头文件 <script src="dist/template-native.js"></script>
(2)设置script相关标签<script id="test" type="text/html">
(3)调用template('test', data);方法实现
</code></pre><p> 示例代码:</p>
<pre><code><script id="test" type="text/html">
<h1><%=title%></h1>
<ul>
<% for(var i = 0, len = list.length; i < len; i++){ %>
<li><%=list[i]%></li>
<% } %>
</ul>
</script>
<script>
var data = {
title: '基本例子',
list: ['文艺1', '博客2', '摄影3', '电影4', '民谣5', '旅行6', '吉他7']
};
var html = template('test', data);
document.body.innerHTML = html;
</script>
</code></pre><p> 模板应用到微博案例中</p>
<pre><code> <script id="nj" type="text/html">
<p class="replyContent"><%=content%></p>
<p class="operation">
<span class="replyTime"><%=time%></span>
<span class="handle">
<a href="javascript:;" class="top"><%=acc%></a>
<a href="javascript:;" class="down_icon"><%=ref%></a>
<a href="javascript:;" class="cut">删除</a>
</span>
</p>
</script>
</code></pre>
</section>
</div>
</div>
</div>
<a href="./index.html" class="navigation navigation-prev " aria-label="Previous page: day03"><i class="fa fa-angle-left"></i></a>
<a href="./2.html" class="navigation navigation-next " aria-label="Next page: cookie存储"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="gitbook/app.js"></script>
<script src="https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script src="gitbook/plugins/gitbook-plugin-mathjax/plugin.js"></script>
<script>
require(["gitbook"], function(gitbook) {
var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
gitbook.start(config);
});
</script>
</body>
</html>