-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
991 lines (864 loc) · 188 KB
/
atom.xml
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Mia</title>
<subtitle>A Dreamer</subtitle>
<link href="/atom.xml" rel="self"/>
<link href="http://yoursite.com/"/>
<updated>2016-07-03T11:53:57.343Z</updated>
<id>http://yoursite.com/</id>
<author>
<name>Mia Wang</name>
</author>
<generator uri="http://hexo.io/">Hexo</generator>
<entry>
<title>一个Flex布局的实例</title>
<link href="http://yoursite.com/2016/07/03/%E4%B8%80%E4%B8%AAFlex%E5%B8%83%E5%B1%80%E7%9A%84%E5%AE%9E%E4%BE%8B/"/>
<id>http://yoursite.com/2016/07/03/一个Flex布局的实例/</id>
<published>2016-07-03T11:53:13.000Z</published>
<updated>2016-07-03T11:53:57.343Z</updated>
<content type="html"><p>Flex布局是一种更有效,更灵活布局方式,主要思想就是赋予容器(container)改变内部子项目(items)的宽度、高度或次序,通过子项目的扩展/收缩,以达到最好的利用可用空间的目的。</p>
<p>Flex还有一个更重要的特点,它是无方向性的(block是垂直向布局,inline是水平向布局),也因而更灵活。</p>
<a id="more"></a>
<h3 id="1-属性简介"><a href="#1-属性简介" class="headerlink" title="1.属性简介"></a>1.属性简介</h3><p>Flex布局中定义了很多属性,这些属性可以分为两类,一类是container属性,另一类是items属性。</p>
<ul>
<li>container属性:diplay flex-direction flex-wrap flex-flow justify-content align-items align-content</li>
<li>items属性:order flex-grow flex-shrink flex-basis flex align-self</li>
</ul>
<h3 id="2-一个实例"><a href="#2-一个实例" class="headerlink" title="2.一个实例"></a>2.一个实例</h3><p>这里用一个例子说明部分属性的使用。例子的布局要求来自这里<a href="http://ife.baidu.com/task/detail?taskId=10" target="_blank" rel="external">http://ife.baidu.com/task/detail?taskId=10</a></p>
<p>Demo见<a href="http://codepen.io/IridescentMia/pen/RRgwgW" target="_blank" rel="external">CodePen</a></p>
<p>下面梳理例子中用到的几个属性:</p>
<ul>
<li>justify-content: space-between; 用于调整四个item在水平方向的分布。justify-content取值有如下5种,默认是flex-start。</li>
</ul>
<p><img src="http://o77yvgv4e.bkt.clouddn.com/160703justifycontent.png" alt=""></p>
<ul>
<li>align-items: center; 用于高度方向的中间对齐。align-items取值有如下5中,默认为stretch;baseline指的是项目的第一行文字基线对齐</li>
</ul>
<p><img src="http://o77yvgv4e.bkt.clouddn.com/160703alignitems.png" alt=""></p>
<ul>
<li><p>flex-wrap: wrap; 当一行排不下时换行。flex-wrap的取值有三个:nowrap(默认)/wrap/wrap-reverse</p>
</li>
<li><p>order: -1; 控制items的排序,默认值为0,值越小越靠前。</p>
</li>
</ul>
<hr>
<p>【参考】<br><a href="https://css-tricks.com/snippets/css/a-guide-to-flexbox/" target="_blank" rel="external">A Complete Guide to Flexbox</a><br><a href="http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html" target="_blank" rel="external">Flex 布局教程:语法篇</a><br><a href="http://www.ruanyifeng.com/blog/2015/07/flex-examples.html" target="_blank" rel="external">Flex 布局教程:实例篇</a></p>
</content>
<summary type="html">
<p>Flex布局是一种更有效,更灵活布局方式,主要思想就是赋予容器(container)改变内部子项目(items)的宽度、高度或次序,通过子项目的扩展/收缩,以达到最好的利用可用空间的目的。</p>
<p>Flex还有一个更重要的特点,它是无方向性的(block是垂直向布局,inline是水平向布局),也因而更灵活。</p>
</summary>
</entry>
<entry>
<title>SHSubway项目笔记4-mongoDB+JavaScript</title>
<link href="http://yoursite.com/2016/06/20/SHSubway%E9%A1%B9%E7%9B%AE%E7%AC%94%E8%AE%B04-mongoDB-JavaScript/"/>
<id>http://yoursite.com/2016/06/20/SHSubway项目笔记4-mongoDB-JavaScript/</id>
<published>2016-06-20T11:06:19.000Z</published>
<updated>2016-06-20T11:06:46.956Z</updated>
<content type="html"><p>目录结构</p>
<ul>
<li>1.mongoDB执行js脚本</li>
<li>2.mongoDB大量删除数据空间释放问题</li>
<li>3.JavaScript中深复制和浅复制</li>
</ul>
<a id="more"></a>
<h3 id="1-mongoDB执行js脚本"><a href="#1-mongoDB执行js脚本" class="headerlink" title="1.mongoDB执行js脚本 "></a><strong>1.mongoDB执行js脚本 </strong></h3><p>一直用roboMongo做mongoDB的管理,界面很清晰很好用,直到执行运行时间很久很耗内存的命令,roboMongo一次又一次的崩溃,发现还是shell靠谱抗造。</p>
<p>mongoDB的shell是JavaScript实现的,用shell执行js脚本方便稳定。</p>
<ul>
<li>将js脚本保存到mongoDB的bin目录下</li>
<li>命令行工具进入bin目录,执行以下操作</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mongo <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">27017</span>/dbName –quiet test.js</span><br></pre></td></tr></table></figure>
<p>然后就静静的等待运行结束吧~</p>
<p>【参考】<br><a href="http://blog.csdn.net/zzq900503/article/details/46414115" target="_blank" rel="external">mongodb执行js脚本(一)—shell执行</a></p>
<h3 id="2-mongoDB大量删除数据空间释放问题"><a href="#2-mongoDB大量删除数据空间释放问题" class="headerlink" title="2.mongoDB大量删除数据空间释放问题"></a><strong>2.mongoDB大量删除数据空间释放问题</strong></h3><p>在删除数据的过程中,发现一个奇怪的现象,原本数据库25.5G,有413562171个文档。我执行了下面的操作:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.collectionName.remove( &#123; xxx: <span class="string">"xxx"</span> &#125; )</span><br></pre></td></tr></table></figure>
<p>这个操作删除了165540416个文档之后,数据库的大小竟然不减反加,变成25.8G。这让我非常困惑。</p>
<p>【原因】<br>在Stack Overflow提问,原来是mongoDB不会自动释放空间,它认为这些空间在以后新建文档的时候还能用到。</p>
<p>【手动释放空间】<br>如果想释放这些空间,可以使用repairDatabase</p>
<p>mongo shell命令为:<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.repairDatabase()</span><br></pre></td></tr></table></figure></p>
<p>或者命令行工具在mongoDB安装bin目录下执行<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mongod --repair</span><br></pre></td></tr></table></figure></p>
<p>【参考】<br><a href="http://stackoverflow.com/questions/37499195/why-the-mongodb-database-take-more-space-after-removing-documents" target="_blank" rel="external">Why the mongodb database take more space after removing documents</a></p>
<h3 id="3-JavaScript中深复制和浅复制"><a href="#3-JavaScript中深复制和浅复制" class="headerlink" title="3.JavaScript中深复制和浅复制"></a><strong>3.JavaScript中深复制和浅复制</strong></h3><p>先看如下代码:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> original = &#123;&#125;;</span><br><span class="line">original.a = <span class="string">'abc'</span>;</span><br><span class="line"><span class="keyword">var</span> copy = original;</span><br><span class="line">copy.a = <span class="string">'xxx'</span>;</span><br><span class="line"><span class="built_in">console</span>.log(original.a);<span class="comment">// 输出xxx</span></span><br></pre></td></tr></table></figure>
<p>新建对象copy,并将对象original赋值给对象copy,然后修改copy的属性a,发现对象original的属性被改变。</p>
<p>【原因】<br>因为JavaScript存储对象都是存地址的,所以以上复制会导致original和copy指向同一块内存地址,以上对象的复制方式为浅复制。</p>
<p>如果想复制后修改属性,而不改变原对象属性值,可以采用深复制方式,开辟一块新的内存地址,将原对象的各个属性<strong>逐个复制</strong>过去。</p>
<p>深复制可以使用jQuery的$.extend()函数实现,如下:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> x = &#123;</span><br><span class="line"> a: <span class="number">1</span>,</span><br><span class="line"> b: &#123; f: &#123; g: <span class="number">1</span> &#125; &#125;,</span><br><span class="line"> c: [ <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span> ]</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> y = $.extend(&#123;&#125;, x), <span class="comment">// 浅复制</span></span><br><span class="line"> z = $.extend(<span class="literal">true</span>, &#123;&#125;, x); <span class="comment">// 深复制</span></span><br><span class="line"></span><br><span class="line">y.b.f === x.b.f <span class="comment">// true</span></span><br><span class="line">z.b.f === x.b.f <span class="comment">// false</span></span><br></pre></td></tr></table></figure>
<p>【参考】</p>
<p><a href="https://www.zhihu.com/question/23031215" target="_blank" rel="external">javascript中的深拷贝和浅拷贝?</a><br><a href="http://jerryzou.com/posts/dive-into-deep-clone-in-javascript/" target="_blank" rel="external">深入剖析 JavaScript 的深复制</a></p>
</content>
<summary type="html">
<p>目录结构</p>
<ul>
<li>1.mongoDB执行js脚本</li>
<li>2.mongoDB大量删除数据空间释放问题</li>
<li>3.JavaScript中深复制和浅复制</li>
</ul>
</summary>
</entry>
<entry>
<title>小tips-CSS篇1</title>
<link href="http://yoursite.com/2016/06/16/%E5%B0%8Ftips-CSS%E7%AF%871/"/>
<id>http://yoursite.com/2016/06/16/小tips-CSS篇1/</id>
<published>2016-06-16T13:34:46.000Z</published>
<updated>2016-06-16T13:37:49.416Z</updated>
<content type="html"><h3 id="1-block-vs-inline-vs-inline-block"><a href="#1-block-vs-inline-vs-inline-block" class="headerlink" title="1.block vs inline vs inline-block"></a><strong>1.block vs inline vs inline-block</strong></h3><p>常用的inline元素有:a span img input br select small strong等</p>
<p>常用的block元素有:div p h1-h6 form table ul ol等</p>
<a id="more"></a>
<p>三者的区别比较:<br><strong>display:block</strong></p>
<ul>
<li>独占一行,多个block元素各自新起一行</li>
<li>可以设置width, height属性。宽度默认为父元素宽度,高度默认为内容高度</li>
<li>可以设置margin, padding属性</li>
</ul>
<p><strong>display:inline</strong></p>
<ul>
<li>多个相邻元素排列在同一行,直到一行排不下,会换到下一行</li>
<li>设置width, height无效。宽度随内容变化,高度默认为一行</li>
<li>设置margin,padding水平方向有效,垂直方向无效</li>
</ul>
<p><strong>display:inline-block</strong></p>
<p>将对象呈递为inline,但是对象的内容作为block呈递</p>
<ul>
<li>多个相邻元素排列在同一行,直到一行排不下,会换到下一行</li>
<li>可以设置width, height属性</li>
<li>可以设置margin, padding属性</li>
</ul>
<h3 id="2-box-sizing"><a href="#2-box-sizing" class="headerlink" title="2.box-sizing"></a><strong>2.box-sizing</strong></h3><p>box-sizing是CSS3属性,其值有三个选择content-box|border-box|inherit</p>
<table>
<thead>
<tr>
<th style="text-align:left">值</th>
<th style="text-align:right">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">content-box(默认值)</td>
<td style="text-align:right">Width and height values apply to the element’s content only. The padding and border are added to the outside of the box.</td>
</tr>
<tr>
<td style="text-align:left">padding-box</td>
<td style="text-align:right">Width and height values apply to the element’s content and its padding. The border is added to the outside of the box. Currently</td>
</tr>
<tr>
<td style="text-align:left">border-box</td>
<td style="text-align:right">Width and height values apply to the content, padding, and border.</td>
</tr>
<tr>
<td style="text-align:left">inherit</td>
<td style="text-align:right">inherits the box sizing of the parent element.</td>
</tr>
</tbody>
</table>
<p>[兼容性]</p>
<p>ie8+浏览器支持content-box和border-box;</p>
<p>ff则支持全部三个值。</p>
<p>[参考]<br><a href="https://css-tricks.com/almanac/properties/b/box-sizing/" target="_blank" rel="external">CSS-Tricks: box-sizing</a></p>
<h3 id="3-box-shadow"><a href="#3-box-shadow" class="headerlink" title="3.box-shadow"></a><strong>3.box-shadow</strong></h3><p>box-shadow是CSS3属性</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">box-shadow</span>: 3<span class="selector-tag">px</span> 3<span class="selector-tag">px</span> 5<span class="selector-tag">px</span> 6<span class="selector-tag">px</span> <span class="selector-id">#ccc</span>;</span><br></pre></td></tr></table></figure>
<p>以上5个参数依次为:水平偏移,垂直偏移,模糊半径(可选参数),传播半径<br>(可选参数),阴影颜色</p>
<p>利用box-shadow属性可以实现多边框效果,效果图如下:</p>
<p><img src="http://o77yvgv4e.bkt.clouddn.com/160616boxshadow.png" alt=""></p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">box-shadow</span>: 0 2<span class="selector-tag">px</span> 2<span class="selector-tag">px</span> <span class="selector-id">#616161</span>,</span><br><span class="line"> 0 0 0 20<span class="selector-tag">px</span> <span class="selector-id">#cecece</span>,</span><br><span class="line"> 0 0 0 24<span class="selector-tag">px</span> <span class="selector-id">#fff</span>,</span><br><span class="line"> 0 0 0 28<span class="selector-tag">px</span> <span class="selector-id">#666</span>,</span><br><span class="line"> <span class="selector-tag">inset</span> 0 50<span class="selector-tag">px</span> 0 <span class="selector-tag">rgba</span>(255,255,255,<span class="selector-class">.2</span>);</span><br></pre></td></tr></table></figure>
</content>
<summary type="html">
<h3 id="1-block-vs-inline-vs-inline-block"><a href="#1-block-vs-inline-vs-inline-block" class="headerlink" title="1.block vs inline vs inline-block"></a><strong>1.block vs inline vs inline-block</strong></h3><p>常用的inline元素有:a span img input br select small strong等</p>
<p>常用的block元素有:div p h1-h6 form table ul ol等</p>
</summary>
</entry>
<entry>
<title>SHSubway项目笔记3-D3+LeafLet绘图</title>
<link href="http://yoursite.com/2016/05/27/SHSubway%E9%A1%B9%E7%9B%AE%E7%AC%94%E8%AE%B03-D3-LeafLet%E7%BB%98%E5%9B%BE/"/>
<id>http://yoursite.com/2016/05/27/SHSubway项目笔记3-D3-LeafLet绘图/</id>
<published>2016-05-27T13:29:18.000Z</published>
<updated>2016-05-27T13:32:40.603Z</updated>
<content type="html"><p>最近又把之前读的论文找出来重读,寄希望在反复阅读中能找到灵感,这个过程有点难过,就像刚刚接触一样东西,盲人摸象一样,好像知道它大概是个什么,又深觉不得要领。</p>
<p>而读论文又不像写代码,可以明确的知道写了多少行,做了什么效果。成果很难衡量,所以最近常常苦恼。</p>
<p>下面继续整理最近解决的问题,结构目录如下:</p>
<ul>
<li>1.在leaflet绘制的地图上,用D3添加元素</li>
<li>2.JavaScript的Date对象,加上一段时间</li>
<li>3.line chart的转折点的光滑处理</li>
<li>4.D3坐标轴样式</li>
</ul>
<a id="more"></a>
<h3 id="1-在leaflet绘制的地图上,用D3添加元素"><a href="#1-在leaflet绘制的地图上,用D3添加元素" class="headerlink" title="1.在leaflet绘制的地图上,用D3添加元素"></a><strong>1.在leaflet绘制的地图上,用D3添加元素</strong></h3><p>需求就是像下图这样,在渲染好的地图上使用svg绘制各个地铁站点以及线路的信息。</p>
<p><img src="http://o77yvgv4e.bkt.clouddn.com/160527map.png" alt=""></p>
<p>已有的数据是各个站点的经纬度坐标,问题就是怎样将这些坐标对应到地图上相应的点。</p>
<p>这里用到了leaflet的一个函数latLngToLayerPoint</p>
<p>这是一篇专门介绍latLngToLayerPoint使用的博客:<br><a href="http://bl.ocks.org/d3noob/9267535" target="_blank" rel="external">http://bl.ocks.org/d3noob/9267535</a></p>
<p>在使用这个函数的过程中,我遇到了一个问题,随着地图的缩小,原来相对精准的点偏移的越来越严重,最后飘到了海里去……</p>
<p>后来发现是理解上的错误,我原以为latLngToLayerPoint是在点原有坐标(31.xxx,121.xxx)的基础上做偏移,这样就需要在circle元素上设置cx, cy,然后再transform,像下面这样:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">stations</span><br><span class="line"> .enter()</span><br><span class="line"> .append(<span class="string">'circle'</span>)</span><br><span class="line"> .attr(<span class="string">'class'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">d</span>) </span>&#123;</span><br><span class="line"> <span class="keyword">return</span> d.id;</span><br><span class="line"> &#125;)</span><br><span class="line"> .attr(<span class="string">'fill'</span>, <span class="string">"blue"</span>)</span><br><span class="line"> .attr(<span class="string">'cx'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">d</span>) </span>&#123;</span><br><span class="line"> <span class="keyword">return</span> d.x;</span><br><span class="line"> &#125;)<span class="comment">// 注意这里设置了cx</span></span><br><span class="line"> .attr(<span class="string">'cy'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">d</span>) </span>&#123;</span><br><span class="line"> <span class="keyword">return</span> d.y;</span><br><span class="line"> &#125;)<span class="comment">// 注意这里设置了cy</span></span><br><span class="line"> .attr(<span class="string">'r'</span>, <span class="number">5</span>);</span><br></pre></td></tr></table></figure>
<p>这里补充说明一点,当circle元素的cx, cy未设置的时候,circle的圆心在页面的坐标原点上。</p>
<p>其实,latLngToLayerPoint是相对于页面的原点偏移的,即不需要设置cx, cy即可,所以我相当于在精确位置上,多加了cx, cy,当地图很大的时候,这个值微乎其微;而当地图缩到很小的时候,就严重偏移了。去掉上面代码中注释的两个attr就恢复正常了。</p>
<h3 id="2-JavaScript的Date对象,加上一段时间"><a href="#2-JavaScript的Date对象,加上一段时间" class="headerlink" title="2.JavaScript的Date对象,加上一段时间"></a><strong>2.JavaScript的Date对象,加上一段时间</strong></h3><p>如题,我想在下面time上增加一小时</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> time = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="string">'2016 5 27 22:20:00 +0800'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(time); <span class="comment">//Fri May 27 2016 22:20:00 GMT+0800 (中国标准时间)</span></span><br></pre></td></tr></table></figure>
<p>于是,我就想到了这样:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> time = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="string">'2016 5 27 22:20:00 +0800'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(time + <span class="number">3600000</span>); <span class="comment">// Fri May 27 2016 22:20:00 GMT+0800 (中国标准时间)3600000</span></span><br></pre></td></tr></table></figure>
<p>如你所见,它真的就在末尾加了3600000ms ,输出了这样一串 Fri May 27 2016 22:20:00 GMT+0800 (中国标准时间)3600000</p>
<p>搜了一下这个问题,解决办法如下:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> time = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="string">'2016 5 27 22:20:00 +0800'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(time.getTime() + <span class="number">3600000</span>); <span class="comment">// 1464362400000</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="keyword">new</span> <span class="built_in">Date</span>(time.getTime() + <span class="number">3600000</span>)); <span class="comment">// Fri May 27 2016 23:20:00 GMT+0800 (中国标准时间)</span></span><br></pre></td></tr></table></figure>
<h3 id="3-line-chart的转折点的光滑处理"><a href="#3-line-chart的转折点的光滑处理" class="headerlink" title="3.line chart的转折点的光滑处理"></a><strong>3.line chart的转折点的光滑处理</strong></h3><p>先看一下对比图</p>
<p><img src="http://o77yvgv4e.bkt.clouddn.com/160527sharp.png" alt=""><br><img src="http://o77yvgv4e.bkt.clouddn.com/160527smooth.png" alt=""></p>
<p>原生的图转折点很锋利,而D3中圆滑化处理也很方便,只需要加.interpolate(“basis”),如下:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> line = d3.svg.line()</span><br><span class="line"> .interpolate(<span class="string">"basis"</span>) <span class="comment">// &lt;=== THERE IT IS!</span></span><br><span class="line"> .x(<span class="function"><span class="keyword">function</span>(<span class="params">d</span>) </span>&#123; <span class="keyword">return</span> x(d.date); &#125;)</span><br><span class="line"> .y(<span class="function"><span class="keyword">function</span>(<span class="params">d</span>) </span>&#123; <span class="keyword">return</span> y(d.close); &#125;);</span><br></pre></td></tr></table></figure>
<p><a href="http://www.d3noob.org/2013/01/smoothing-out-lines-in-d3js.html" target="_blank" rel="external">Smoothing out the lines in d3.js</a>这篇文章有更细致的说明</p>
<h3 id="4-D3坐标轴样式"><a href="#4-D3坐标轴样式" class="headerlink" title="4.D3坐标轴样式"></a><strong>4.D3坐标轴样式</strong></h3><p>之前看到百度地图的<a href="http://jiaotong.baidu.com.cn/static/drive/nj-taxi.html" target="_blank" rel="external">出租车OD平台</a>,很喜欢他的数据图。<br><img src="http://o77yvgv4e.bkt.clouddn.com/160527axis.png" alt=""></p>
<p>刚巧在D3的官网上发现了一组坐标轴样式,其中一个可以实现上图的效果,更多样式见:<br><a href="https://github.com/d3/d3/wiki/SVG-Axes" target="_blank" rel="external">https://github.com/d3/d3/wiki/SVG-Axes</a></p>
<hr>
<p>【参考资料】<br><a href="http://stackoverflow.com/questions/1197928/how-to-add-30-minutes-to-a-javascript-date-object" target="_blank" rel="external">How to add 30 minutes to a JavaScript Date object?</a></p>
</content>
<summary type="html">
<p>最近又把之前读的论文找出来重读,寄希望在反复阅读中能找到灵感,这个过程有点难过,就像刚刚接触一样东西,盲人摸象一样,好像知道它大概是个什么,又深觉不得要领。</p>
<p>而读论文又不像写代码,可以明确的知道写了多少行,做了什么效果。成果很难衡量,所以最近常常苦恼。</p>
<p>下面继续整理最近解决的问题,结构目录如下:</p>
<ul>
<li>1.在leaflet绘制的地图上,用D3添加元素</li>
<li>2.JavaScript的Date对象,加上一段时间</li>
<li>3.line chart的转折点的光滑处理</li>
<li>4.D3坐标轴样式</li>
</ul>
</summary>
</entry>
<entry>
<title>SHSubway项目笔记2-mongoDB+node.js边用边学</title>
<link href="http://yoursite.com/2016/05/20/SHSubway%E9%A1%B9%E7%9B%AE%E7%AC%94%E8%AE%B02-mongoDB-node-js%E8%BE%B9%E7%94%A8%E8%BE%B9%E5%AD%A6/"/>
<id>http://yoursite.com/2016/05/20/SHSubway项目笔记2-mongoDB-node-js边用边学/</id>
<published>2016-05-20T11:36:00.000Z</published>
<updated>2016-06-24T01:49:23.698Z</updated>
<content type="html"><p>从五一假期回来到今天5月20号,从node.js和mongoDB的小白,到现在基本完成了第一步的目标。截图留念。</p>
<p><img src="http://o77yvgv4e.bkt.clouddn.com/0520.png" alt=""></p>
<a id="more"></a>
<p>这半个月来,就像坐着过山车,一直处于遇到问题的苦闷和解决问题的激动中,一个问题的结束就是另一个问题的开始。中间遇到问题找不到答案的时候,也曾怀疑过用一个自己完全不熟悉的数据库是不是太不明智,不过问题解决了,就又愉快的用起了mongoDB,谁让自己好奇呢。</p>
<p>下面整理我跨越的那些问题:</p>
<p>目录结构:</p>
<ul>
<li>1.mongoDB索引操作</li>
<li>2.循环中的回调函数</li>
<li>3.ejs里面输出json对象给js文件调用</li>
</ul>
<h3 id="1-mongoDB索引操作"><a href="#1-mongoDB索引操作" class="headerlink" title="1.mongoDB索引操作"></a><strong>1.mongoDB索引操作</strong></h3><p>其中一个功能需要大量查询,如果不做优化全表扫描,页面的等待时间很长。</p>
<p>先用两个查询做对比:</p>
<p>建索引前:<br>9.269s,这还是在工作站上跑出了的结果,自己的电脑就悲剧了。<br><img src="http://o77yvgv4e.bkt.clouddn.com/ensureIndex02.png" alt=""></p>
<p>建索引后:<br>0.003s,毫秒级查询速度快的飞起。<br><img src="http://o77yvgv4e.bkt.clouddn.com/ensureIndex01.png" alt=""></p>
<p>这里用到了mongoDB的索引,基本的索引操作整理如下:</p>
<ul>
<li>建立索引:</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在name字段上升序(1)建立索引</span></span><br><span class="line">db.collection.ensureIndex(&#123;<span class="string">"name"</span>:<span class="number">1</span>&#125;);</span><br><span class="line"><span class="comment">// 在name字段上降序(-1)建立索引</span></span><br><span class="line">db.collection.ensureIndex(&#123;<span class="string">"name"</span>:<span class="number">-1</span>&#125;);</span><br><span class="line"><span class="comment">// 建立复合索引,先根据name升序,再根据age升序,适合查询中有多个查询条件的情况</span></span><br><span class="line">db.collection.ensureIndex(&#123;<span class="string">"name"</span>:<span class="number">1</span>,<span class="string">"age"</span>:<span class="number">1</span>&#125;);</span><br></pre></td></tr></table></figure>
<ul>
<li>查询索引</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.collection.getIndexes();</span><br></pre></td></tr></table></figure>
<ul>
<li>删除索引</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.collection.dropIndex(&#123;<span class="string">"name"</span>:<span class="number">1</span>&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="2-循环中的回调函数"><a href="#2-循环中的回调函数" class="headerlink" title="2.循环中的回调函数"></a><strong>2.循环中的回调函数</strong></h3><p>这是异步操作中常见的一个陷阱,for循环中的回调函数内,使用for的计数器i,像下面这样:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, len = arr.length; i &lt; len; i++) &#123;</span><br><span class="line"> ...</span><br><span class="line"> model.count(query, <span class="function"><span class="keyword">function</span>(<span class="params">err, data</span>) </span>&#123;</span><br><span class="line"> <span class="built_in">console</span>.log(i);</span><br><span class="line"> <span class="built_in">console</span>.log(arr[i]);</span><br><span class="line"> &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>结果如下:</p>
<blockquote>
<p>3<br>undefined<br>3<br>undefined<br>3<br>undefined</p>
</blockquote>
<p>即i一直输出len的值,也就是循环结束后的值,因此也导致arr[i]下标超出数组的大小,故一直输出undefined。</p>
<p>【解决办法】</p>
<ul>
<li>利用JavaScript函数编程特性,手动建立闭包</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>, len = arr.length; i &lt; len; i++) &#123;</span><br><span class="line"> ...</span><br><span class="line"> (<span class="function"><span class="keyword">function</span>(<span class="params">i</span>) </span>&#123;</span><br><span class="line"> model.count(query, <span class="function"><span class="keyword">function</span>(<span class="params">err, data</span>) </span>&#123;</span><br><span class="line"> <span class="built_in">console</span>.log(i);</span><br><span class="line"> <span class="built_in">console</span>.log(arr[i]);</span><br><span class="line"> &#125;);</span><br><span class="line"> &#125;)(i);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此方法虽然解决了回调函数内使用i的问题,但是多个model.count函数并行执行,并不能保证执行按照循环的顺序进行。</p>
<ul>
<li>数组的foreach循环</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line">arr.foreach(<span class="function"><span class="keyword">function</span>(<span class="params">num</span>)</span>&#123;</span><br><span class="line"> ...</span><br><span class="line"> model.count(query, <span class="function"><span class="keyword">function</span>(<span class="params">err, data</span>) </span>&#123;</span><br><span class="line"> <span class="built_in">console</span>.log(num);</span><br><span class="line"> &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p><a href="http://www.cnblogs.com/gaojun/p/4157684.html" target="_blank" rel="external">更详细解释</a></p>
<h3 id="3-ejs里面输出json对象给js文件调用"><a href="#3-ejs里面输出json对象给js文件调用" class="headerlink" title="3.ejs里面输出json对象给js文件调用"></a><strong>3.ejs里面输出json对象给js文件调用</strong></h3><p>现在呢,终于顺利的从数据库查询到想要的数据,也顺利的传给了ejs页面,那么怎么继续向前传,传给js文件来展示这些数据呢?</p>
<p>首先我们看看传过来的数据,服务器端代码如下:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">router.post(<span class="string">'/'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">req, res, next</span>) </span>&#123;</span><br><span class="line"> res.render(<span class="string">'index'</span>,&#123;info:&#123;<span class="string">"a"</span>:<span class="number">1</span>,<span class="string">"b"</span>:<span class="number">2</span>&#125;&#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>客户端js文件读取传递过来的信息,如下:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span><span class="javascript"></span><br><span class="line"> <span class="keyword">var</span> data = <span class="string">'&lt;%- JSON.stringify(info) %&gt;'</span>;</span><br><span class="line"> json = <span class="built_in">JSON</span>.parse(data);</span><br><span class="line"> <span class="built_in">console</span>.log(data[<span class="string">"a"</span>]);</span><br><span class="line"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<hr>
<p>【参考资料】<br><a href="http://www.cnblogs.com/huangxincheng/category/355399.html" target="_blank" rel="external">8天学通MongoDB</a><br><a href="https://segmentfault.com/q/1010000002780259" target="_blank" rel="external">ejs里面怎么输出json对象给页面的js调用</a></p>
</content>
<summary type="html">
<p>从五一假期回来到今天5月20号,从node.js和mongoDB的小白,到现在基本完成了第一步的目标。截图留念。</p>
<p><img src="http://o77yvgv4e.bkt.clouddn.com/0520.png" alt=""></p>
</summary>
<category term="mongoDB" scheme="http://yoursite.com/tags/mongoDB/"/>
<category term="node.js" scheme="http://yoursite.com/tags/node-js/"/>
</entry>
<entry>
<title>SHSubway项目笔记-mongoDB+node.js边用边学</title>
<link href="http://yoursite.com/2016/05/17/SHSubway%E9%A1%B9%E7%9B%AE%E7%AC%94%E8%AE%B0-mongoDB-node-js%E8%BE%B9%E7%94%A8%E8%BE%B9%E5%AD%A6/"/>
<id>http://yoursite.com/2016/05/17/SHSubway项目笔记-mongoDB-node-js边用边学/</id>
<published>2016-05-17T13:13:41.000Z</published>
<updated>2016-05-17T13:14:26.959Z</updated>
<content type="html"><p>最近在用mongoDB搭建可视化系统的数据库,记录一下使用过程中解决的问题</p>
<p>目录结构:</p>
<ul>
<li>1.将csv文件导入到mongoDB</li>
<li>2.使用mongoose建Model时,集合名称自动加s变复数的问题</li>
<li>3.mongoDB数据库中将时间从字符串格式转换成Date日期格式</li>
<li>4.循环集合更新文档中的某些字段**</li>
<li>5.node.js+mongoDB教程**</li>
</ul>
<a id="more"></a>
<h3 id="1-将csv文件导入到mongoDB"><a href="#1-将csv文件导入到mongoDB" class="headerlink" title="1.将csv文件导入到mongoDB"></a><strong>1.将csv文件导入到mongoDB</strong></h3><p>手头的原始数据是csv格式的,要用mongoDB数据库首先遇到的问题就是这样导入数据,使用mongoimport命令即可,具体命令如下:</p>
<blockquote>
<p>mongoimport -d users -c contacts –type csv –file path/of/file/to/import.csv</p>
</blockquote>
<p>其中:<br>-d: 即–db的缩写,将csv文件导入的数据库<br>-c: 即–collection的缩写,csv在数据库中保存的集合名称</p>
<p>更多可选参数信息见<a href="https://docs.mongodb.com/manual/reference/program/mongoimport/" target="_blank" rel="external">官方文档</a></p>
<ul>
<li>如果csv文件第一行是字段名称,则使用–headerline</li>
</ul>
<blockquote>
<p>mongoimport -d users -c contacts –file /path/of/file/to/import.csv –headerline</p>
</blockquote>
<ul>
<li>如果csv文件没有字段名称,则使用–fieldFile另行指定字段名称所在文件,字段名文件中,每行写入一个字段,不需要用逗号分隔,字段文件的格式可以为csv, txt等。</li>
</ul>
<blockquote>
<p>mongoimport -d users -c contacts –file /path/of/file/to/import.csv –fieldFile /path/of/fieldfile.csv</p>
</blockquote>
<p>【!】这里我遇到一个问题,在怎么运行以上命令都报错,SyntaxError: missing ; before statement。这时只要退出shell即可,以上命令是在mongoDB安装目录下运行的,而不是shell中。</p>
<p>导入json,tsv格式的文件也是类似的操作,详见<a href="https://docs.mongodb.com/manual/reference/program/mongoimport/" target="_blank" rel="external">官方文档</a></p>
<h3 id="2-使用mongoose建Model时,集合名称自动加s变复数的问题"><a href="#2-使用mongoose建Model时,集合名称自动加s变复数的问题" class="headerlink" title="2.使用mongoose建Model时,集合名称自动加s变复数的问题"></a><strong>2.使用mongoose建Model时,集合名称自动加s变复数的问题</strong></h3><p>【问题描述】</p>
<p>在使用mongoose连接mongoDB数据库的过程中,先建Schema,再建Model,分别如下:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line"><span class="comment">//建Schema</span></span><br><span class="line"><span class="keyword">var</span> MovieSchema = <span class="keyword">new</span> mongoose.Schema(&#123;</span><br><span class="line"> name: <span class="built_in">String</span>,</span><br><span class="line"> password: <span class="built_in">String</span></span><br><span class="line">&#125;);</span><br><span class="line">...</span><br><span class="line"><span class="comment">//建Model</span></span><br><span class="line"><span class="keyword">var</span> Movie = mongoose.model(<span class="string">'Movie'</span>, MovieSchema);</span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<p>这样写本以为会添加到数据库Movie集合中,实际操作时会在数据库中新建集合movies,并完成添加操作。</p>
<p>如果建Model时,如下这样写,则会正常添加到movies集合中(没有movies,则创建并添加)。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> Movie = mongoose.model(<span class="string">'movies'</span>, MovieSchema);</span><br></pre></td></tr></table></figure>
<p>【原因分析】</p>
<p>由以上现象可以推断mongoose在内部创建collection时将我们传递的collection名小写化,同时如果小写化的名称后面没有字母s,则会在其后面添加s,因此出现创建集合movies。</p>
<p>【解决办法】</p>
<p>只要在创建Schema和Model的时候稍加改动,就可以免去小写化和自动+s的困扰,创建方法如下:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> MovieSchema = <span class="keyword">new</span> mongoose.Schema(&#123;</span><br><span class="line"> name: <span class="built_in">String</span>,</span><br><span class="line"> password: <span class="built_in">String</span></span><br><span class="line">&#125;,&#123;collection:<span class="string">'movie'</span>&#125;);</span><br><span class="line"><span class="keyword">var</span> card = mongoose.model(<span class="string">'movie'</span>, MovieSchema , <span class="string">'movie'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="3-mongoDB数据库中将时间从字符串格式转换成Date日期格式"><a href="#3-mongoDB数据库中将时间从字符串格式转换成Date日期格式" class="headerlink" title="3.mongoDB数据库中将时间从字符串格式转换成Date日期格式"></a><strong>3.mongoDB数据库中将时间从字符串格式转换成Date日期格式</strong></h3><p>在数据库中,有同是String类型的日期字段和时间字段,如下图,现在想把它们组装成一个Date类型的字段。<br><img src="http://o77yvgv4e.bkt.clouddn.com/dateTime.png" alt=""></p>
<p>最开始的时候我是这样写的</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> newDate = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="string">"2015-04-01 19:20:33"</span>);</span><br></pre></td></tr></table></figure>
<p>但是一直提示Invalid Date,百思不得解,直到我在Stack Overflow上看到这个问题<a href="http://stackoverflow.com/questions/10942931/converting-string-to-date-in-mongodb" target="_blank" rel="external">Converting string to date in mongodb</a>,原来是字符串拼接的问题。</p>
<p>下面是可以正常完成转换的格式中的一种<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> newDate = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="string">"2015 04 01 19:20:33 +0800"</span>);</span><br></pre></td></tr></table></figure></p>
<p>格式可以正常转换了,但是有遇到另外一个问题,转换前后的数据是下图这样的</p>
<p><img src="http://o77yvgv4e.bkt.clouddn.com/dateTime.png" alt=""><br><img src="http://o77yvgv4e.bkt.clouddn.com/convertDate.png" alt=""></p>
<p>转换后的时间刚好少了8小时,而UTC(世界标准时间)刚好比CST(中国标准时间)晚8小时,即Robomongo的默认显示是时间标准时间,可以在设置中调整为所在地时区,调整方法如下图<br><img src="http://o77yvgv4e.bkt.clouddn.com/timezone.png" alt=""></p>
<p>调整后的时间显示,如下图</p>
<p><img src="http://o77yvgv4e.bkt.clouddn.com/dateTime0800.png" alt=""></p>
<h3 id="4-循环集合更新文档中的某些字段"><a href="#4-循环集合更新文档中的某些字段" class="headerlink" title="4.循环集合更新文档中的某些字段"></a><strong>4.循环集合更新文档中的某些字段</strong></h3><p>转换一个doc的时间问题解决了,那么怎么更新正个集合的时间呢,最开始我的想法是这样的</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">db.collectionName.find(&#123;&#125;).forEach(<span class="function"><span class="keyword">function</span> (<span class="params">doc</span>) </span>&#123;</span><br><span class="line"> doc.dateTime = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="string">"dateJoinTIme"</span>);</span><br><span class="line"> print( doc.dateTime );</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>但是shell报错</p>
<blockquote>
<p>TypeError: db.getCollection(…).find(…).foreach is not a function</p>
</blockquote>
<p>【解决办法】使用 <a href="https://docs.mongodb.org/manual/reference/method/Bulk/#Bulk" target="_blank" rel="external">“Bulk”</a></p>
<p>具体的使用方法如下,这里用到了我在3中提到的转换时间的方法:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> collection = db.getCollection(<span class="string">'test'</span>);</span><br><span class="line"><span class="keyword">var</span> bulkOp = collection.initializeOrderedBulkOp();</span><br><span class="line"><span class="keyword">var</span> count = <span class="number">0</span>;</span><br><span class="line">collection.find().forEach(<span class="function"><span class="keyword">function</span>(<span class="params">doc</span>) </span>&#123;</span><br><span class="line"> <span class="comment">//重新组装new Date()中的时间字符串</span></span><br><span class="line"> <span class="keyword">var</span> dd = doc.date.split(<span class="string">'-'</span>);</span><br><span class="line"> <span class="keyword">var</span> newDate = <span class="keyword">new</span> <span class="built_in">Date</span>(dd[<span class="number">0</span>]+<span class="string">' '</span>+dd[<span class="number">1</span>]+<span class="string">' '</span>+dd[<span class="number">2</span>]+<span class="string">' '</span>+<span class="string">':'</span>+doc.time+<span class="string">' +0800'</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//更新dateTime字段,没有此字段则添加字段</span></span><br><span class="line"> bulkOp.find(&#123; <span class="string">'_id'</span>: doc._id &#125;).updateOne(&#123;</span><br><span class="line"> <span class="string">'$set'</span>: &#123; <span class="string">'dateTime'</span>: newDate &#125;</span><br><span class="line"> &#125;);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//删除文档中的某个字段</span></span><br><span class="line"> bulkOp.find(&#123; <span class="string">'_id'</span>: doc._id &#125;).updateOne(&#123;</span><br><span class="line"> <span class="string">'$unset'</span>: &#123; <span class="string">'time'</span>: doc.time &#125;</span><br><span class="line"> &#125;);</span><br><span class="line"></span><br><span class="line"> count++;</span><br><span class="line"> <span class="keyword">if</span> (count % <span class="number">100</span> === <span class="number">0</span>) &#123;</span><br><span class="line"> <span class="comment">// 每100次操作后执行一次,并完成bulkOp的初始化</span></span><br><span class="line"> bulkOp.execute();</span><br><span class="line"> bulkOp = collection.initializeOrderedBulkOp();</span><br><span class="line"> &#125;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// 清理队列</span></span><br><span class="line"><span class="keyword">if</span> (count &gt; <span class="number">0</span>) &#123;</span><br><span class="line"> bulkOp.execute();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="5-node-js-mongoDB教程"><a href="#5-node-js-mongoDB教程" class="headerlink" title="5.node.js+mongoDB教程"></a><strong>5.node.js+mongoDB教程</strong></h3><p>在不断的遇到问题解决问题的过程中,查了一些书,不得不感慨技术更新换代太快,技术前进的速度远远超过了书的翻译速度,网上教程帮的很大忙,下面列举的这些教程不能说是完美无缺,但是在我搭建数据库和后台系统的过程中给了我很大的帮助,思路清晰明了,帮助我在极短的时间内快速上手。</p>
<p>mongoDB: <a href="http://www.cnblogs.com/huangxincheng/category/355399.html" target="_blank" rel="external">8篇mongoDB教程</a></p>
<p>express+mongoose: <a href="http://www.cnblogs.com/zhongweiv/tag/Nodejs/" target="_blank" rel="external">目前14篇,还在更的一套比较完整的教程</a></p>
<p>node.js+mongoose: <a href="https://github.com/airuikun/mongoose_crud" target="_blank" rel="external">一套简单的实现增删改查的样例</a></p>
<hr>
<p>【参考资料】<br><a href="https://docs.mongodb.com/manual/reference/program/mongoimport/" target="_blank" rel="external">mongoimport</a><br><a href="https://cnodejs.org/topic/4f71363f8a04d82a3d1e4aea" target="_blank" rel="external">CNode社区</a><br><a href="http://stackoverflow.com/questions/33530357/loop-through-mongo-collection-and-update-a-field-in-every-document/33530844#33530844" target="_blank" rel="external">4循环更新集合中的文档-Stack Overflow</a></p>
</content>
<summary type="html">
<p>最近在用mongoDB搭建可视化系统的数据库,记录一下使用过程中解决的问题</p>
<p>目录结构:</p>
<ul>
<li>1.将csv文件导入到mongoDB</li>
<li>2.使用mongoose建Model时,集合名称自动加s变复数的问题</li>
<li>3.mongoDB数据库中将时间从字符串格式转换成Date日期格式</li>
<li>4.循环集合更新文档中的某些字段**</li>
<li>5.node.js+mongoDB教程**</li>
</ul>
</summary>
<category term="mongoDB" scheme="http://yoursite.com/tags/mongoDB/"/>
<category term="node.js" scheme="http://yoursite.com/tags/node-js/"/>
</entry>
<entry>
<title>Cookie简介及相关操作</title>
<link href="http://yoursite.com/2016/05/15/Cookie%E7%AE%80%E4%BB%8B%E5%8F%8A%E7%9B%B8%E5%85%B3%E6%93%8D%E4%BD%9C/"/>
<id>http://yoursite.com/2016/05/15/Cookie简介及相关操作/</id>
<published>2016-05-15T13:29:28.000Z</published>
<updated>2016-05-15T13:39:32.128Z</updated>
<content type="html"><p>今天来介绍下Cookie的相关知识及操作,包括设置Cookie的设置,读取和删除等。</p>
<blockquote>
<p><a href="http://blog.csdn.net/fangaoxin/article/details/6952954" target="_blank" rel="external"><strong>什么是Cookie</strong></a><br>由于HTTP是一种无状态的协议,服务器单从网络连接上无从知道客户身份。怎么办呢?就<strong>给客户端们颁发一个通行证吧,每人一个,无论谁访问都必须携带自己通行证。这样服务器就能从通行证上确认客户身份了。这就是Cookie的工作原理</strong>。<br>Cookie实际上是一小段的文本信息。客户端请求服务器,如果服务器需要记录该用户状态,就使用response向客户端浏览器颁发一个Cookie。客户端浏览器会把Cookie保存起来。当浏览器再请求该网站时,浏览器把请求的网址连同该Cookie一同提交给服务器。服务器检查该Cookie,以此来辨认用户状态。服务器还可以根据需要修改Cookie的内容。</p>
</blockquote>
<a id="more"></a>
<p>文章结构:</p>
<p>1.Cookie的查看方式<br>2.Cookie的特性<br>3.设置Cookie<br>4.读取Cookie<br>5.删除Cookie</p>
<h3 id="1-Cookie的查看方式"><a href="#1-Cookie的查看方式" class="headerlink" title="1.Cookie的查看方式"></a>1.Cookie的查看方式</h3><ul>
<li>在要查看Cookie的网页上输入</li>
</ul>
<p>JavaScript: alert (document. cookie)</p>
<ul>
<li>firefox浏览器任意页面下,右键单击,选择“查看页面信息”,之后在弹出的页面上选择带金色小锁的“安全”,在该标签页下点击“查看Cookie”。</li>
</ul>
<h3 id="2-Cookie的特性"><a href="#2-Cookie的特性" class="headerlink" title="2.Cookie的特性"></a>2.Cookie的特性</h3><ul>
<li>同一个网站共享同一套cookie。</li>
<li>Cookie的大小和条数受限,因浏览器不同而各异,大小在4k-10k左右,一般不会超过50条。<a href="http://www.cnblogs.com/henryhappier/archive/2011/03/03/1969564.html" target="_blank" rel="external">具体大小限制</a></li>
<li>Cookie添加的时候会指定过期时间,超过这一时间即被删除。不指定过期时间,则浏览器关闭过期。</li>
</ul>
<h3 id="3-设置Cookie"><a href="#3-设置Cookie" class="headerlink" title="3.设置Cookie"></a>3.设置Cookie</h3><p>在这里先说一下Cookie的赋值,赋值形式为键值对,用“=”赋值:键=值。</p>
<p>当对Cookie重复添加键值对的时候,不会覆盖前一个</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.cookie=<span class="string">'user=mia'</span>;</span><br><span class="line"><span class="built_in">document</span>.cookie=<span class="string">'pwd=123'</span>;<span class="comment">//user=mia仍保留</span></span><br></pre></td></tr></table></figure>
<p>设置Cookie的函数</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//name为键值对的键,value为值,iDay为过期时间</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">setCookie</span>(<span class="params">name, value, iDay</span>) </span>&#123;</span><br><span class="line"> <span class="keyword">var</span> oDate = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line"> oDate.setDate(oDate.getDate()+iDay);<span class="comment">//iDay天后过期</span></span><br><span class="line"> <span class="built_in">document</span>.cookie = name + <span class="string">'='</span> + value + <span class="string">';expires='</span> + oDate;<span class="comment">//用expires设置过期时间</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="4-读取Cookie"><a href="#4-读取Cookie" class="headerlink" title="4.读取Cookie"></a>4.读取Cookie</h3><p>读取的过程即为分割Cookie字符串的过程<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getCookie</span>(<span class="params">name</span>)</span>&#123;</span><br><span class="line"> <span class="keyword">var</span> arr = <span class="built_in">document</span>.cookie.split(<span class="string">'; '</span>);<span class="comment">//注意这里是分号+一个空格</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; arr.length; i++)&#123;</span><br><span class="line"> <span class="keyword">var</span> arr2 = arr[i].split(<span class="string">'='</span>);</span><br><span class="line"> <span class="keyword">if</span>(arr2[<span class="number">0</span>] == name)&#123;</span><br><span class="line"> <span class="keyword">return</span> arr2[<span class="number">1</span>];</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">return</span> <span class="string">''</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="5-删除Cookie"><a href="#5-删除Cookie" class="headerlink" title="5.删除Cookie"></a>5.删除Cookie</h3><p>删除Cookie用到了一个小技巧,将Cookie的过期时间设置为负值,即过去的某个时间已过期,就完成了Cookie的删除。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">removeCookie</span>(<span class="params">name</span>)</span>&#123;</span><br><span class="line"> setCookie(name,<span class="number">1</span>,<span class="number">-1</span>);<span class="comment">//已过期</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<hr>
<p>【参考资料】<br><a href="http://bbs.zhinengshe.com/thread-1210-1-1.html" target="_blank" rel="external">智能社JavaScript视频教程</a></p>
</content>
<summary type="html">
<p>今天来介绍下Cookie的相关知识及操作,包括设置Cookie的设置,读取和删除等。</p>
<blockquote>
<p><a href="http://blog.csdn.net/fangaoxin/article/details/6952954"><strong>什么是Cookie</strong></a><br>由于HTTP是一种无状态的协议,服务器单从网络连接上无从知道客户身份。怎么办呢?就<strong>给客户端们颁发一个通行证吧,每人一个,无论谁访问都必须携带自己通行证。这样服务器就能从通行证上确认客户身份了。这就是Cookie的工作原理</strong>。<br>Cookie实际上是一小段的文本信息。客户端请求服务器,如果服务器需要记录该用户状态,就使用response向客户端浏览器颁发一个Cookie。客户端浏览器会把Cookie保存起来。当浏览器再请求该网站时,浏览器把请求的网址连同该Cookie一同提交给服务器。服务器检查该Cookie,以此来辨认用户状态。服务器还可以根据需要修改Cookie的内容。</p>
</blockquote>
</summary>
<category term="JavaScript" scheme="http://yoursite.com/tags/JavaScript/"/>
</entry>
<entry>
<title>地图坐标体系介绍及其互转工具</title>
<link href="http://yoursite.com/2016/05/14/%E5%9C%B0%E5%9B%BE%E5%9D%90%E6%A0%87%E4%BD%93%E7%B3%BB%E4%BB%8B%E7%BB%8D%E5%8F%8A%E5%85%B6%E4%BA%92%E8%BD%AC%E5%B7%A5%E5%85%B7/"/>
<id>http://yoursite.com/2016/05/14/地图坐标体系介绍及其互转工具/</id>
<published>2016-05-14T12:06:53.000Z</published>
<updated>2016-05-14T12:07:43.912Z</updated>
<content type="html"><p>今天在用leaflet的时候发现,手头数据的经纬度坐标是基于百度地图体系的,在leaflet中用openstreetmap会有很大的偏移,解决了偏移问题,顺便了解了一下不同地图使用的坐标体系,整理如下:</p>
<p>先说一说现有的坐标体系:</p>
<a id="more"></a>
<ul>
<li><strong>WGS-84/地球坐标</strong></li>
</ul>
<p>使用者:谷歌(除去中国部分),OpenStreetMap<br>原始坐标体系。很可惜,在中国,任何一个地图产品都不允许使用GPS坐标。保密。</p>
<ul>
<li><strong>GCJ-02/火星坐标/国测局坐标</strong></li>
</ul>
<p>使用者:谷歌(中国地图部分),腾讯,高德<br>在中国,必须至少使用GCJ-02的坐标体系。GCJ-02也是国内最广泛使用的坐标体系。</p>
<ul>
<li><strong>BD-09/百度坐标</strong></li>
</ul>
<p>使用者:百度<br>百度公司独有的坐标系</p>
<p>下面介绍几个坐标转换的工具:</p>
<ul>
<li><a href="https://www.npmjs.com/package/coordtransform" target="_blank" rel="external">coordtransform</a>: 基于Node.js</li>
<li><a href="https://github.com/wandergis/coordTransform_py" target="_blank" rel="external">coordTransform_py</a>: 基于Python版本</li>
<li><a href="http://prog3.com/sbdm/blog/longshengguoji/article/details/49128545" target="_blank" rel="external">GCJ02 BD09 and WGS84 coordinate system conversion code</a>: 基于JavaScript的野生版本</li>
</ul>
<p>看到定位点终于回到了它原本的位置,真是一身轻松呢~</p>
<hr>
<p>【参考资料】<br><a href="https://www.npmjs.com/package/coordtransform" target="_blank" rel="external">coordtransform</a><br><a href="https://segmentfault.com/a/1190000000498434" target="_blank" rel="external">【高德地图API】如何解决坐标转换,坐标偏移?</a><br><a href="http://prog3.com/sbdm/blog/longshengguoji/article/details/49128545" target="_blank" rel="external">GCJ02 BD09 and WGS84 coordinate system conversion code</a></p>
</content>
<summary type="html">
<p>今天在用leaflet的时候发现,手头数据的经纬度坐标是基于百度地图体系的,在leaflet中用openstreetmap会有很大的偏移,解决了偏移问题,顺便了解了一下不同地图使用的坐标体系,整理如下:</p>
<p>先说一说现有的坐标体系:</p>
</summary>
<category term="tools" scheme="http://yoursite.com/tags/tools/"/>
</entry>
<entry>
<title>JavaScript中的正则表达式</title>
<link href="http://yoursite.com/2016/05/08/JavaScript%E4%B8%AD%E7%9A%84%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/"/>
<id>http://yoursite.com/2016/05/08/JavaScript中的正则表达式/</id>
<published>2016-05-08T08:56:26.000Z</published>
<updated>2016-05-16T08:23:25.947Z</updated>
<content type="html"><p>最近一直用到正则表达式,今天跟着教程系统学习了一下,整理笔记如下:</p>
<h3 id="1-字符串操作函数"><a href="#1-字符串操作函数" class="headerlink" title="1.字符串操作函数"></a><strong>1.字符串操作函数</strong></h3><ul>
<li>search</li>
<li>substring</li>
<li>charAt</li>
<li>split</li>
</ul>
<p><strong>1.1 search</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str=<span class="string">'abcdef'</span>;</span><br><span class="line">alert(str.search(<span class="string">'b'</span>));<span class="comment">//1, 找到则返回所在位置</span></span><br></pre></td></tr></table></figure>
<a id="more"></a>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str=<span class="string">'abcdef'</span>;</span><br><span class="line">alert(str.search(<span class="string">'m'</span>));<span class="comment">//-1, 没找到返回-1</span></span><br></pre></td></tr></table></figure>
<p><strong>1.2 substring</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str=<span class="string">'abcdef'</span>;</span><br><span class="line">alert(str.substring(<span class="number">2</span>,<span class="number">4</span>)); <span class="comment">//cd, 返回[2,4)</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str=<span class="string">'abcdef'</span>;</span><br><span class="line">alert(str.substring(<span class="number">2</span>)); <span class="comment">//cdef, 只提供一个参数时,取到字符串结尾</span></span><br></pre></td></tr></table></figure>
<p><strong>1.3 charAt</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str=<span class="string">'abcdef'</span>;</span><br><span class="line">alert(str.charAt(<span class="number">3</span>)); <span class="comment">//d</span></span><br></pre></td></tr></table></figure>
<p><strong>1.4 split</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str=<span class="string">'ab-cd-ef'</span>;</span><br><span class="line">alert(str.split(<span class="string">'-'</span>)); <span class="comment">//[ab,cd,ef]</span></span><br></pre></td></tr></table></figure>
<h3 id="2-正则表达式"><a href="#2-正则表达式" class="headerlink" title="2.正则表达式"></a><strong>2.正则表达式</strong></h3><ul>
<li>两种书写方式</li>
<li>标志的介绍</li>
<li>元字符介绍</li>
<li>量词的介绍</li>
<li>方括号介绍</li>
</ul>
<p><strong>2.1 两种书写方式</strong></p>
<ul>
<li>perl风格(字面量形式):</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> re = <span class="regexp">/[bc]at/i</span>; <span class="comment">// re = /pattern/attributes;</span></span><br></pre></td></tr></table></figure>
<ul>
<li>Javascript风格(使用RegExp构造函数定义):</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> re = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"[bc]at"</span>,<span class="string">"i"</span>); <span class="comment">// re = new RegExp(pattern, attributes);</span></span><br></pre></td></tr></table></figure>
<p><strong>2.2 标志的介绍</strong></p>
<ul>
<li>i: 不区分大小写模式(case-insensitive)</li>
<li>g: 全局模式(global),不是在发现第一个匹配项时立即停止</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'abc 853 isaf98024 sfk'</span>;</span><br><span class="line"><span class="keyword">var</span> re1 = <span class="regexp">/\d/</span>;</span><br><span class="line"><span class="keyword">var</span> re2 = <span class="regexp">/\d/g</span>;</span><br><span class="line">alert(str.match(re1));<span class="comment">//8</span></span><br><span class="line">alert(str.match(re2));<span class="comment">//8,5,3,9,8,0,2,4</span></span><br></pre></td></tr></table></figure>
<p><strong>2.3 元字符介绍</strong></p>
<p><strong>\d</strong> :匹配数字</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'abc 853 isaf98024 sfk9we 98'</span>;</span><br><span class="line"><span class="keyword">var</span> re = <span class="regexp">/\d/</span>;</span><br><span class="line">alert(str.search(re));<span class="comment">//4 第一个数字出现的位置</span></span><br></pre></td></tr></table></figure>
<p><strong>\D</strong> :匹配非数字,等价于[^0-9]</p>
<p><strong>\w</strong> :匹配英文、数字,下划线,等价于[a-z0-9_]</p>
<p><strong>\W</strong> :匹配除了英文、数字,下划线以外的字符,等价于[^a-z0-9_]</p>
<p><strong>\s</strong> :匹配空白字符</p>
<p><strong>\S</strong> :匹配非空白字符</p>
<p><strong>.(点)</strong> :匹配任意字符,尽量少用</p>
<p><strong>2.4 量词的介绍</strong></p>
<p><strong>{n}</strong> :正好出现n次</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> re = \d&#123;<span class="number">8</span>&#125;; <span class="comment">// 匹配连续8位数字</span></span><br></pre></td></tr></table></figure>
<p><strong>{n,m}</strong> :最少n次,最多m次<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> re = [<span class="number">1</span><span class="number">-9</span>]\d&#123;<span class="number">4</span>,<span class="number">10</span>&#125;;<span class="comment">//5-11位非0开头的QQ号</span></span><br></pre></td></tr></table></figure></p>
<p><strong>{n,}</strong> :n最少出现n次,最多次数不限</p>
<p><strong>n+</strong> :n至少出现一次,等价于{1,}</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'abc 853 isaf98024 sfk'</span>;</span><br><span class="line"><span class="keyword">var</span> re1 = <span class="regexp">/\d/g</span>;</span><br><span class="line"><span class="keyword">var</span> re2 = <span class="regexp">/\d+/g</span>;</span><br><span class="line">alert(str.match(re1));<span class="comment">//8,5,3,9,8,0,2,4</span></span><br><span class="line">alert(str.match(re2));<span class="comment">//853,98024</span></span><br></pre></td></tr></table></figure>
<p><strong>n?</strong> :n出现一次或者不出现,等价于{0,1}<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//电话号码中以0开头的3-4位区号可有可无,和后面非0开头的8位电话号,用-连接</span></span><br><span class="line"><span class="keyword">var</span> re = <span class="regexp">/(0\d&#123;2,3&#125;-)?[1-9]\d&#123;7&#125;/</span>;</span><br></pre></td></tr></table></figure></p>
<p><strong>n*</strong> :等价于{0,},不推荐使用</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'abc 853 isaf98024 sfk'</span>;</span><br><span class="line"><span class="keyword">var</span> re1 = <span class="regexp">/\d+/g</span>;</span><br><span class="line"><span class="keyword">var</span> re2 = <span class="regexp">/\d*/g</span>;</span><br><span class="line">alert(str.match(re1));<span class="comment">//853,9802</span></span><br><span class="line">alert(str.match(re2));<span class="comment">//,,,,853,,,,,,98024,,,,,</span></span><br></pre></td></tr></table></figure>
<p><strong>^n</strong> :匹配开头为n的字符串<br><strong>n$</strong> :匹配结尾为n的字符串</p>
<h3 id="3-和正则表达式搭配的函数"><a href="#3-和正则表达式搭配的函数" class="headerlink" title="3.和正则表达式搭配的函数"></a><strong>3.和正则表达式搭配的函数</strong></h3><ul>
<li>match</li>
<li>replace</li>
<li>test</li>
</ul>
<p><strong>3.1 match</strong></p>
<p>返回指定的值,<em>而非字符串的位置</em><br> <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'abc 853 isaf98024 sfk'</span>;</span><br><span class="line"><span class="keyword">var</span> re = <span class="regexp">/\d/</span>;</span><br><span class="line">alert(str.match(re));<span class="comment">//8</span></span><br></pre></td></tr></table></figure></p>
<p><strong>3.2 replace</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'abc abc abc'</span>;</span><br><span class="line"><span class="keyword">var</span> re = <span class="regexp">/a/g</span>;</span><br><span class="line">alert(str.replace(re,<span class="string">'0'</span>));<span class="comment">//0bc 0bc 0bc</span></span><br><span class="line"><span class="string">``</span><span class="string">` </span><br><span class="line"></span><br><span class="line">**3.3 test**</span><br><span class="line"></span><br><span class="line">只要string中有re满足的部分即会返回true</span><br><span class="line">`</span><span class="string">``</span> javascript</span><br><span class="line"><span class="comment">//只要string中有re满足的部分即会返回true</span></span><br><span class="line">re.test(string);</span><br></pre></td></tr></table></figure>
<h3 id="4-实例"><a href="#4-实例" class="headerlink" title="4.实例"></a><strong>4.实例</strong></h3><ul>
<li>邮箱校验</li>
<li>去除一段文字中的html标签</li>
</ul>
<p><strong>4.1 邮箱校验</strong></p>
<p>邮箱的拼写规则:英文/数字/下划线&nbsp;&nbsp;&nbsp;&nbsp;@&nbsp;&nbsp;&nbsp;&nbsp;英文/数字 &nbsp;&nbsp;&nbsp;&nbsp;.&nbsp;&nbsp;&nbsp;&nbsp;英文<br>邮箱的正则表达式:/&nbsp;&nbsp;&nbsp;&nbsp;\w+&nbsp;&nbsp;&nbsp;&nbsp;@&nbsp;&nbsp;&nbsp;&nbsp;[a-z0-9]+&nbsp;&nbsp;&nbsp;&nbsp;\.&nbsp;&nbsp;&nbsp;&nbsp;[a-z]+&nbsp;&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;&nbsp;i</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> re = <span class="regexp">/\w+@[a-z0-9]+\.[a-z]+/i</span>;</span><br><span class="line">re.test(<span class="string">'[email protected]'</span>);<span class="comment">//true</span></span><br><span class="line">re.test(<span class="string">'[email protected]啦啦啦'</span>);<span class="comment">//true</span></span><br></pre></td></tr></table></figure>
<p>注意到第二次校验的邮箱并不符合格式要求,但是结果仍然返回true。这是因为test函数,只要string中有正则表达式满足的部分即会返回true。</p>
<p>对正则表达式限制首部和尾部,可以避免这一问题,此时只有string从第一个字符到最后一个字符完全匹配成功,才会返回true,修改后的正则表达式如下:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> re = <span class="regexp">/^\w+@[a-z0-9]+\.[a-z]+$/i</span>;</span><br></pre></td></tr></table></figure>
<p><strong>4.2 去除一段文字中的html标签</strong></p>
<p>如下一段文字中,多次出现html标签,在这个例子中,我们用正则表达式匹配每个标签,方便进一步对标签做删除操作。</p>
<blockquote>
<p>&lt;p&gt; 正是盛夏时节,不比初春时的一片新绿,知道好日子才开始,所以明亮快活,眼前的绿是沉甸甸的,许是因为知道绚烂已到了顶,以后的日子只有每况愈下。一如我此时的心情。&lt;br&gt;&lt;br&gt; 十天前,我换灯泡时从梯子上摔下来,醒时已经是在这具身体前主人的床上了。据丫鬟说,我是从阁楼的楼梯上摔了下来,然后昏迷了一天一夜。&lt;br&gt; &lt;br&gt; </p>
</blockquote>
<p>取出html标签,即取出&lt;&gt;包裹的内容,于是又如下正则表达式:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> re = <span class="regexp">/&lt;.+&gt;/g</span>;</span><br></pre></td></tr></table></figure>
<p>但是此时取出的是上面完整的段落,造成这样的原因是<strong>正则表达式的贪婪性</strong>,即会取出满足表达式的尽可能长的字符串,于是从段首的“&lt;”开始,一直取到断尾的“&gt;”。</p>
<p>正则表达式中 .+ 描述的不准确,并不是“&lt;&gt;”中任何字符都可以出现,其自身就不可以出现,修改正则表达式为:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> re = <span class="regexp">/&lt;[^&lt;&gt;]+&gt;/g</span>;</span><br></pre></td></tr></table></figure>
<p>———————- 2016年5月16日更新 ———————-</p>
<h3 id="5-正则表达式中包含变量"><a href="#5-正则表达式中包含变量" class="headerlink" title="5.正则表达式中包含变量"></a><strong>5.正则表达式中包含变量</strong></h3><p>今天写正则的时候遇到一个问题,我希望得到的正则表达式是下面这样的,其中stations[i]是个字符串变量<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> re = <span class="regexp">/.*stations[i]/</span>;</span><br></pre></td></tr></table></figure></p>
<p>然而这样写的结果,得到的正则真的是就是原样的 /.*stations[i]/,stations[i]被当成了一个字符串处理了,而不是变量。</p>
<p>找到的解决办法是使用JavaScript风格的书写方式,如下:<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> re = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">".*"</span>+stations[i]);</span><br></pre></td></tr></table></figure></p>
<p>OK~就是这么简单!</p>
<hr>
<p><strong>参考资料:</strong></p>
<p><a href="http://bbs.zhinengshe.com/thread-1211-1-1.html" target="_blank" rel="external">智能社JavaScript视频教程-32.JS中的正则表达式</a><br><a href="http://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/001434499503920bb7b42ff6627420da2ceae4babf6c4f2000" target="_blank" rel="external">廖雪峰JavaScript教程-RegExp</a><br><a href="http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp" target="_blank" rel="external">W3School-JavaScript RegExp 对象</a></p>
</content>
<summary type="html">
<p>最近一直用到正则表达式,今天跟着教程系统学习了一下,整理笔记如下:</p>
<h3 id="1-字符串操作函数"><a href="#1-字符串操作函数" class="headerlink" title="1.字符串操作函数"></a><strong>1.字符串操作函数</strong></h3><ul>
<li>search</li>
<li>substring</li>
<li>charAt</li>
<li>split</li>
</ul>
<p><strong>1.1 search</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str=<span class="string">'abcdef'</span>;</span><br><span class="line">alert(str.search(<span class="string">'b'</span>));<span class="comment">//1, 找到则返回所在位置</span></span><br></pre></td></tr></table></figure>
</summary>
<category term="JavaScript" scheme="http://yoursite.com/tags/JavaScript/"/>
<category term="前端" scheme="http://yoursite.com/tags/%E5%89%8D%E7%AB%AF/"/>
</entry>
<entry>
<title>addEventListener vs onclick</title>
<link href="http://yoursite.com/2016/03/28/onclick/"/>
<id>http://yoursite.com/2016/03/28/onclick/</id>
<published>2016-03-28T12:46:01.000Z</published>
<updated>2016-03-28T12:49:02.009Z</updated>
<content type="html"><p>今天来说一说addEventListener来添加事件和直接添加的区别,下面用“onclick”事件作说明。</p>
<p>形式1:element.onclick = function () { }; //等价于内联的事件<br>形式2:element.addEventListener(‘click’, function () { } , false); //最后一个参数控制冒泡事件,为false则阻止事件冒泡。</p>
<a id="more"></a>
<h4 id="区别"><a href="#区别" class="headerlink" title="区别"></a><em>区别</em></h4><p>当对一个element绑定多个相同的事件时:</p>
<p>形式1:只执行最后绑定的那个事件,前面绑定的事件被覆盖。下面的例子中,只alert ‘b’.<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">element.onclick = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; alert(<span class="string">'a'</span>); &#125;;</span><br><span class="line">element.onclick = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; alert(<span class="string">'b'</span>); &#125;;</span><br></pre></td></tr></table></figure></p>
<p>形式2:依次执行多个绑定的事件。下面的例子中,先后alert ‘a’ 和 ‘b’.<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">element.addEventListener(<span class="string">'click'</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;alert(<span class="string">'a'</span>);&#125;,<span class="literal">false</span>);</span><br><span class="line">element.addEventListener(<span class="string">'click'</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;alert(<span class="string">'b'</span>);&#125;,<span class="literal">false</span>);</span><br></pre></td></tr></table></figure></p>
<h4 id="addEventListener的兼容性"><a href="#addEventListener的兼容性" class="headerlink" title="addEventListener的兼容性"></a><em>addEventListener的兼容性</em></h4><p>低于ie9的ie浏览器不支持addEventListener, 需要使用attachEvent<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">element.attachEvent(<span class="string">'onclick'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; &#125;);</span><br></pre></td></tr></table></figure></p>
<p>兼容的写法:</p>
<ul>
<li><p>使用jQuery</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$(element).on(&apos;click&apos;, function () &#123; /* do stuff */ &#125;);</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用javascript做兼容性处理</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">addEvent</span>(<span class="params">element, evnt, funct</span>)</span>&#123;</span><br><span class="line"> <span class="keyword">if</span> (element.attachEvent)</span><br><span class="line"> <span class="keyword">return</span> element.attachEvent(<span class="string">'on'</span>+evnt, funct);</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">return</span> element.addEventListener(evnt, funct, <span class="literal">false</span>);</span><br><span class="line">&#125;</span><br><span class="line">addEvent(</span><br><span class="line"> <span class="built_in">document</span>.getElementById(<span class="string">'myElement'</span>),</span><br><span class="line"> <span class="string">'click'</span>,</span><br><span class="line"> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; &#125;</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>【参考】<a href="http://stackoverflow.com/questions/6348494/addeventlistener-vs-onclick" target="_blank" rel="external">http://stackoverflow.com/questions/6348494/addeventlistener-vs-onclick</a></p>
</content>
<summary type="html">
<p>今天来说一说addEventListener来添加事件和直接添加的区别,下面用“onclick”事件作说明。</p>
<p>形式1:element.onclick = function () { }; //等价于内联的事件<br>形式2:element.addEventListener(‘click’, function () { } , false); //最后一个参数控制冒泡事件,为false则阻止事件冒泡。</p>
</summary>
<category term="前端" scheme="http://yoursite.com/tags/%E5%89%8D%E7%AB%AF/"/>
<category term="js" scheme="http://yoursite.com/tags/js/"/>
</entry>
<entry>
<title>百度前端学院-task22-三步实现二叉树的遍历</title>
<link href="http://yoursite.com/2016/03/22/%E7%99%BE%E5%BA%A6%E5%89%8D%E7%AB%AF%E5%AD%A6%E9%99%A2-task22-Js%E5%AE%9E%E7%8E%B0%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%81%8D%E5%8E%86/"/>
<id>http://yoursite.com/2016/03/22/百度前端学院-task22-Js实现二叉树的遍历/</id>
<published>2016-03-22T10:44:12.000Z</published>
<updated>2016-03-22T13:17:19.380Z</updated>
<content type="html"><p>今天完成了百度前端技术培训的task22,实现了二叉树的三种遍历办法,并以动画的形式呈现,顺便复习了一下二叉树,学习了flexbox,试用了scss。</p>
<p>源码地址:<a href="https://github.com/IridescentMia/IFE-Mia/tree/gh-pages/task22-Binary%20Tree" target="_blank" rel="external">https://github.com/IridescentMia/IFE-Mia/tree/gh-pages/task22-Binary%20Tree</a></p>
<p>demo: <a href="http://iridescentmia.github.io/IFE-Mia/task22-Binary%20Tree/" target="_blank" rel="external">http://iridescentmia.github.io/IFE-Mia/task22-Binary%20Tree/</a></p>
<a id="more"></a>
<h4 id="第一步:定义遍历函数"><a href="#第一步:定义遍历函数" class="headerlink" title="第一步:定义遍历函数"></a><strong>第一步:定义遍历函数</strong></h4><p>理解三种遍历</p>
<ul>
<li>前序遍历:父节点-左孩子节点-右孩子节点</li>
<li>中序遍历:左孩子节点-父节点-右孩子节点</li>
<li>后序遍历:左孩子节点-右孩子节点-父节点</li>
</ul>
<p>用递归的方式实现很容易理解:<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">preOrder</span>(<span class="params">node</span>)</span>&#123;</span><br><span class="line"> <span class="keyword">if</span>(tree !== <span class="literal">null</span>)&#123;</span><br><span class="line"> <span class="built_in">console</span>.log(node);</span><br><span class="line"> preOrder(node.firstElementChild);</span><br><span class="line"> preOrder(node.lastElementChild);</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>中序和后序类似前序,就不贴代码了。</p>
<h4 id="第二步:理解DOM树,完成前序遍历"><a href="#第二步:理解DOM树,完成前序遍历" class="headerlink" title="第二步:理解DOM树,完成前序遍历"></a><strong>第二步:理解DOM树,完成前序遍历</strong></h4><p>刚入手这个题目的时候,一直想着怎么定义节点,怎么定义树,偶然醒悟,DOM里面每个元素都是现成的节点,有node.firstElementChild, node.lastElementChild等指向子节点,DOM本身就是一颗大树~那接下来的工作就容易多了。</p>
<p>获取最顶层的元素,再调用之前写好的函数preOrder就可以实现前序遍历了,如下:<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> domTree = <span class="built_in">document</span>.getElementById(<span class="string">'top-level'</span>);</span><br><span class="line">preOrder(domTree);</span><br></pre></td></tr></table></figure></p>
<h4 id="第三步:以动画的方式呈现"><a href="#第三步:以动画的方式呈现" class="headerlink" title="第三步:以动画的方式呈现"></a><strong>第三步:以动画的方式呈现</strong></h4><p>将前两步的遍历结果存入数组,然后这一步只需循环一遍数组元素,并相应的改变样式即可。</p>
<p>这里用到了setInterval,每一个间隔循环一个数组元素。1.将前一个数组元素回归白色背景;2.将当前元素背景色设置为非白色。</p>
<hr>
<p><small>最后附上两个flex的教程,从入门到精通的利器,强烈推荐</small></p>
<ul>
<li><a href="http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html" target="_blank" rel="external">Flex 布局教程:语法篇</a></li>
<li><a href="http://www.ruanyifeng.com/blog/2015/07/flex-examples.html" target="_blank" rel="external">Flex 布局教程:实例篇</a></li>
</ul>
</content>
<summary type="html">
<p>今天完成了百度前端技术培训的task22,实现了二叉树的三种遍历办法,并以动画的形式呈现,顺便复习了一下二叉树,学习了flexbox,试用了scss。</p>
<p>源码地址:<a href="https://github.com/IridescentMia/IFE-Mia/tree/gh-pages/task22-Binary%20Tree">https://github.com/IridescentMia/IFE-Mia/tree/gh-pages/task22-Binary%20Tree</a></p>
<p>demo: <a href="http://iridescentmia.github.io/IFE-Mia/task22-Binary%20Tree/">http://iridescentmia.github.io/IFE-Mia/task22-Binary%20Tree/</a></p>
</summary>
<category term="前端" scheme="http://yoursite.com/tags/%E5%89%8D%E7%AB%AF/"/>
<category term="IFE" scheme="http://yoursite.com/tags/IFE/"/>
<category term="数据结构" scheme="http://yoursite.com/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/"/>
</entry>
<entry>
<title>百度前端学院-task8小结</title>
<link href="http://yoursite.com/2016/03/21/%E7%99%BE%E5%BA%A6%E5%89%8D%E7%AB%AF%E5%AD%A6%E9%99%A2-task8%E5%B0%8F%E7%BB%93/"/>
<id>http://yoursite.com/2016/03/21/百度前端学院-task8小结/</id>
<published>2016-03-21T10:50:44.000Z</published>
<updated>2016-05-24T06:10:27.993Z</updated>
<content type="html"><p>Hi,又见面了。今天总结的是任务八,使用 HTML 与 CSS 实现类似 BootStrap 的响应式 12 栏网格布局,根据屏幕宽度,元素占的栏数不同。完成基本版本后,我又学习了下scss, 做了一个scss版本。</p>
<a id="more"></a>
<p>代码地址:<a href="https://github.com/IridescentMia/IFE-Mia/tree/master/task8-Responsive%20Grid%20System" target="_blank" rel="external">https://github.com/IridescentMia/IFE-Mia/tree/master/task8-Responsive%20Grid%20System</a></p>
<h4 id="1-CSS3新属性:calc"><a href="#1-CSS3新属性:calc" class="headerlink" title="1. CSS3新属性:calc"></a>1. <strong>CSS3新属性:calc</strong></h4><p>之前就遇到过这样的问题,用100%的宽度减去固定像素的宽度,今天在一瞥别人的代码,竟然遇到它,真是相见恨晚呢。</p>
<p>【功能】混合各种单位进行运算,如百分比、px、em、rem等</p>
<p>【示例】<br><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.box</span>&#123; <span class="attribute">border</span>:<span class="number">1px</span> solid <span class="number">#ddd</span>; <span class="attribute">width</span>:<span class="built_in">calc</span>(100%-2px) &#125;</span><br></pre></td></tr></table></figure></p>
<p>容器宽度加上边框宽度正好100%。</p>
<p>【参考】<a href="https://www.qianduan.net/calc-at-at-at-page-intelligent-layout/" target="_blank" rel="external">css3 calc()功能小窥</a></p>
<h4 id="2-CSS3新属性:box-sizing"><a href="#2-CSS3新属性:box-sizing" class="headerlink" title="2. CSS3新属性:box-sizing"></a>2. <strong>CSS3新属性:box-sizing</strong></h4><p>属性值:</p>
<ul>
<li>content-box(默认):border和padding不计算入width之内</li>
<li>padding-box:padding计算入width内</li>
<li>border-box:border和padding计算入width之内</li>
</ul>
<h4 id="3-CSS3新增选择器"><a href="#3-CSS3新增选择器" class="headerlink" title="3. CSS3新增选择器"></a>3. <strong>CSS3新增选择器</strong></h4><p>在如下的html文档中选中所有class中包含’col-‘的 标签<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"col-md-4 col-sm-6"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"col-md-4 col-sm-6"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"col-md-4 col-sm-12"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>是时候用CSS3强大的选择器了<br><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-attr">[class*='col-']</span>&#123;&#125;</span><br></pre></td></tr></table></figure></p>
<p>整理几个实用的属性选择器如下:</p>
<ol>
<li>img[alt]:匹配含有alt的img标签</li>
<li>img[alt=’p’]:匹配alt=’p’的img标签</li>
<li>img[alt^=’p’]:匹配alt以’p’开头的img标签</li>
<li>img[alt*=’p’]:匹配alt包含’p’的img标签</li>
<li>img[alt$=’p’]:匹配alt以’p’结尾的img标签</li>
</ol>
<p>伪类选择器:</p>
<ol>
<li>li:first-child</li>
<li>li:last-child</li>
<li>li:nth-child(2n+3):从第三个元素开始每2个元素选择1个</li>
<li>li:not(.a):没有a类的列表项</li>
</ol>
<p>【参考】《响应式Web设计 HTML5和CSS3实战》</p>
<h4 id="4-SCSS"><a href="#4-SCSS" class="headerlink" title="4. SCSS"></a>4. <strong>SCSS</strong></h4><h6 id="4-1-混合宏-vs-继承-vs-占位符"><a href="#4-1-混合宏-vs-继承-vs-占位符" class="headerlink" title="4.1. 混合宏 vs 继承 vs 占位符"></a>4.1. 混合宏 vs 继承 vs 占位符</h6><ul>
<li>混合宏:可以传递参数,不会合并相同样式代码</li>
<li>继承:不能传参,会合并相同样式代码</li>
<li>占位符:不能传参,只有调用占位符的时候才会在css中生成代码</li>
</ul>
<h6 id="4-2-默认变量"><a href="#4-2-默认变量" class="headerlink" title="4.2. 默认变量"></a>4.2. 默认变量</h6><p>在默认变量前声明同名变量,则覆盖默认变量<br><figure class="highlight scss"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$baseLineHeight</span>: <span class="number">2</span>;</span><br><span class="line"><span class="variable">$baseLineHeight</span>: <span class="number">1.5</span> !default;</span><br><span class="line"><span class="selector-tag">body</span>&#123;</span><br><span class="line"> <span class="attribute">line-height</span>: <span class="variable">$baseLineHeight</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>【参考】<a href="http://www.imooc.com/code/7041" target="_blank" rel="external">Sass入门篇</a></p>
</content>
<summary type="html">
<p>Hi,又见面了。今天总结的是任务八,使用 HTML 与 CSS 实现类似 BootStrap 的响应式 12 栏网格布局,根据屏幕宽度,元素占的栏数不同。完成基本版本后,我又学习了下scss, 做了一个scss版本。</p>
</summary>
</entry>
<entry>
<title>百度前端学院-task7小结</title>
<link href="http://yoursite.com/2016/03/18/%E7%99%BE%E5%BA%A6%E5%89%8D%E7%AB%AF%E5%AD%A6%E9%99%A2-task7%E5%B0%8F%E7%BB%93/"/>
<id>http://yoursite.com/2016/03/18/百度前端学院-task7小结/</id>
<published>2016-03-18T13:03:10.000Z</published>
<updated>2016-05-24T06:11:27.212Z</updated>
<content type="html"><p>这里是我在<a href="http://ife.baidu.com/" target="_blank" rel="external">百度前端学院</a>学习的小结,我负责的部分是任务七中的part3。</p>
<a id="more"></a>
<p>demo: <a href="http://hellozts4120.github.io/IFE-2016/task1/Project7/index.html" target="_blank" rel="external">http://hellozts4120.github.io/IFE-2016/task1/Project7/index.html</a></p>
<h4 id="1-两个并排的inline-block的div之间存在空隙"><a href="#1-两个并排的inline-block的div之间存在空隙" class="headerlink" title="1. 两个并排的inline-block的div之间存在空隙"></a>1. <strong>两个并排的inline-block的div之间存在空隙</strong></h4><p>当两个div的display设置为inline-block时,它们并不会紧紧的相邻,而是出现一个缝隙。<br><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">div</span>&#123; <span class="attribute">display</span>: inline-block; &#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"div1"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"div2"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>【原因】出现这样的间隙,是因为标签段之间的空格<br>【解决办法】</p>
<ul>
<li><p>去掉标签段之间的空格:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"div1"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"div2"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>使用margin负值</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">div</span>&#123; <span class="attribute">display</span>: inline-block; <span class="attribute">margin-right</span>: -<span class="number">3px</span>; &#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>【参考】<a href="http://www.zhangxinxu.com/wordpress/2012/04/inline-block-space-remove-%E5%8E%BB%E9%99%A4%E9%97%B4%E8%B7%9D/" target="_blank" rel="external">更多解决办法</a></p>
<h4 id="2-div包裹img时,div底部多出3px"><a href="#2-div包裹img时,div底部多出3px" class="headerlink" title="2. div包裹img时,div底部多出3px"></a>2. <strong>div包裹img时,div底部多出3px</strong></h4><p>【解决办法】</p>
<ul>
<li><p>设置display</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">img</span>&#123; <span class="attribute">display</span>: block; &#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>设置vertical-align</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">img</span>&#123; <span class="attribute">vertical-align</span>: top; &#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h4 id="3-多个并排div的height,自动和最高的那个保持一致"><a href="#3-多个并排div的height,自动和最高的那个保持一致" class="headerlink" title="3. 多个并排div的height,自动和最高的那个保持一致"></a>3. <strong>多个并排div的height,自动和最高的那个保持一致</strong></h4><ul>
<li>设置display为table-cell,即让标签元素以表格单元格的形式呈现<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="css"></span><br><span class="line"> <span class="selector-class">.left</span>,</span><br><span class="line"> <span class="selector-class">.right</span> &#123;</span><br><span class="line"> <span class="attribute">padding</span>: <span class="number">10px</span>;</span><br><span class="line"> <span class="attribute">display</span>: table-cell;</span><br><span class="line"> <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#f40</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"wrap"</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"left"</span>&gt;</span>left div<span class="tag">&lt;<span class="name">br</span>/&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"right"</span>&gt;</span>right div<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>【更多关于table-cell】<a href="http://www.zhangxinxu.com/wordpress/2010/10/%E6%88%91%E6%89%80%E7%9F%A5%E9%81%93%E7%9A%84%E5%87%A0%E7%A7%8Ddisplaytable-cell%E7%9A%84%E5%BA%94%E7%94%A8/" target="_blank" rel="external">我所知道的几种display:table-cell的应用</a></p>
<ul>
<li>设置display为-webkit-box<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="css"></span><br><span class="line"> <span class="selector-class">.wrap</span> &#123;</span><br><span class="line"> <span class="attribute">display</span>: -webkit-box;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="selector-class">.left</span>,</span><br><span class="line"> <span class="selector-class">.right</span> &#123;</span><br><span class="line"> <span class="attribute">padding</span>: <span class="number">10px</span>;</span><br><span class="line"> <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#f40</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"wrap"</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"left"</span>&gt;</span>left div<span class="tag">&lt;<span class="name">br</span>/&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"right"</span>&gt;</span>right div<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>【参考】<a href="http://blog.csdn.net/qinshenxue/article/details/44861695" target="_blank" rel="external">css实现两个div并排等高</a></p>
<p>在第七个任务中,为了让《女人与列车》和《西装与斑马袜》的顶角刚好对上,我希望《女人与列车》占据屏幕34%的宽度,需要对此行做如下设置:</p>
<ol>
<li><p>设置包含三列的父元素</p>
<pre><code class="css"><span class="selector-tag">display</span>: <span class="selector-tag">-webkit-box</span>;
<span class="selector-tag">display</span>: <span class="selector-tag">-moz-box</span>;
<span class="selector-tag">display</span>: <span class="selector-tag">box</span>;
</code></pre>
</li>
<li><p>设置前两列的宽度等宽,对前两列添加</p>
<pre><code class="css"><span class="selector-tag">-webkit-box-flex</span><span class="selector-pseudo">:1</span>;
<span class="selector-tag">-moz-box-flex</span><span class="selector-pseudo">:1</span>;
<span class="selector-tag">box-flex</span><span class="selector-pseudo">:1</span>;
</code></pre>
</li>
<li><p>设置图片宽度为34%</p>
<pre><code class="css"><span class="selector-tag">width</span>: 34%;
</code></pre>
</li>
</ol>
<p><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=529761" target="_blank" rel="external">注意此时,bug出现,firefox下使用css3的box模型后,子div宽度设置百分比无效</a></p>
<p>今天就到这里,see you~</p>
</content>
<summary type="html">
<p>这里是我在<a href="http://ife.baidu.com/">百度前端学院</a>学习的小结,我负责的部分是任务七中的part3。</p>
</summary>
</entry>
<entry>
<title>SQL处理数据小结</title>
<link href="http://yoursite.com/2016/03/14/SQL%E4%BD%BF%E7%94%A8%E5%B0%8F%E7%BB%93/"/>
<id>http://yoursite.com/2016/03/14/SQL使用小结/</id>
<published>2016-03-14T05:59:28.000Z</published>
<updated>2016-03-21T11:04:17.063Z</updated>
<content type="html"><p>数据终于处理完了,简直神清气爽,现在想想,整体思路还是比较简单清晰的,只是过程有点艰难,常常让我觉得得多吃点坚果补补脑。以下是整理的这几天解决的问题。</p>
<a id="more"></a>
<h2 id="1-PATINDEX函数1"><a href="#1-PATINDEX函数1" class="headerlink" title="1. PATINDEX函数1"></a><strong>1. PATINDEX函数</strong><a href="http://www.cnblogs.com/moss_tan_jun/archive/2011/11/26/2263982.html" target="_blank" rel="external">1</a></h2><p>查找pattern在expression中出现的位置,起始值为1,查找失败返回0。</p>
<p>几种形式:</p>
<p>-<strong>PATINDEX ( ‘%pattern%’ , expression )</strong>:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">patindex</span>(<span class="string">'%abb%'</span>,<span class="string">'abcaabbeeabb'</span>)</span><br></pre></td></tr></table></figure>
<p>结果5,也就是abb第一次出现的位置。</p>
<p>-<strong>PATINDEX ( ‘%pattern’ , expression )</strong></p>
<p>-<strong>PATINDEX ( ‘pattern%’ , expression )</strong></p>
<p>-<strong>PATINDEX ( ‘pattern’ , expression )</strong></p>
<p>示例:<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">patindex</span>(<span class="string">'%[^0-9]%'</span>,Line)</span><br></pre></td></tr></table></figure></p>
<p>即取出字符串Line中第一位非数字字符的位置</p>
<h2 id="2-SUBSTRING函数"><a href="#2-SUBSTRING函数" class="headerlink" title="2. SUBSTRING函数"></a><strong>2. SUBSTRING函数</strong></h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">SUBSTRING</span>(expression,<span class="keyword">start</span>,<span class="keyword">length</span>)</span><br></pre></td></tr></table></figure>
<p>参数说明:</p>
<p>-<strong>expression</strong>:字符串、二进制字符串、文本、图像、列或包含列的表达式。请勿使用包含聚合函数的表达式。</p>
<p>-<strong>start</strong>:整数或可以隐式转换为 int 的表达式,指定子字符串的开始位置。</p>
<p>-<strong>length</strong>:整数或可以隐式转换为 int 的表达式,指定子字符串的长度。</p>
<p>示例:<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">substring</span>(Line,<span class="number">1</span>,<span class="number">3</span>)</span><br></pre></td></tr></table></figure></p>
<p>从第1位开始,截取3个字符</p>
<h2 id="3-临时表2"><a href="#3-临时表2" class="headerlink" title="3. 临时表2"></a><strong>3. 临时表<a href="http://www.cnblogs.com/chongzi/archive/2011/01/19/1939106.html" target="_blank" rel="external">2</a></strong></h2><p>#代表局部临时表,##代表全局临时表</p>
<p>-创建临时表</p>
<ol>
<li>方法一:</li>
</ol>
<p>create table #临时表名(字段1 约束条件,字段2 约束条件)</p>
<p>create table ##临时表名(字段1 约束条件,字段2 约束条件)</p>
<ol>
<li>方法二:</li>
</ol>
<p>select <em> into #临时表名 from 表名<br>select </em> into ##临时表名 from 表名</p>
<ul>
<li>删除临时表</li>
</ul>
<p>drop table #临时表名;</p>
<p>drop table ##临时表名;</p>
<h2 id="4-向已有表中增加-删除字段"><a href="#4-向已有表中增加-删除字段" class="headerlink" title="4. 向已有表中增加/删除字段"></a><strong>4. 向已有表中增加/删除字段</strong></h2><ul>
<li>添加字段:</li>
</ul>
<p>alter table 表名 add 字段名 类型</p>
<ul>
<li>删除字段:</li>
</ul>
<p>alter table 表名 drop column 字段名</p>
<h2 id="5-循环语句"><a href="#5-循环语句" class="headerlink" title="5. 循环语句"></a><strong>5. 循环语句</strong></h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">declare</span> @i <span class="built_in">int</span></span><br><span class="line"><span class="keyword">set</span> @i=<span class="number">1</span></span><br><span class="line"><span class="keyword">while</span> @i&lt;<span class="number">30</span></span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">test</span> (userid) <span class="keyword">values</span>(@i)</span><br><span class="line"><span class="keyword">set</span> @i=@i+<span class="number">1</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>
<h2 id="6-日期时间的增加3"><a href="#6-日期时间的增加3" class="headerlink" title="6. 日期时间的增加3"></a><strong>6. 日期时间的增加</strong><a href="http://www.w3school.com.cn/sql/func_dateadd.asp" target="_blank" rel="external">3</a></h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DATEADD(datepart,number,date)</span><br></pre></td></tr></table></figure>
<p>参数说明:</p>
<ul>
<li>date:合法的日期表达式</li>
<li>number:添加的间隔数(对于未来的时间,此数是正数,对于过去的时间,此数是负数)</li>
<li>datepart:如yy/mm/dd/hh等,对应的字母缩写见<a href="http://www.w3school.com.cn/sql/func_dateadd.asp" target="_blank" rel="external">3</a></li>
</ul>
<p>示例:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DATEADD(hh,1,time)</span><br></pre></td></tr></table></figure>
<p>time增加1小时</p>
<h2 id="7-datetime类型转date-转time"><a href="#7-datetime类型转date-转time" class="headerlink" title="7. datetime类型转date/转time"></a><strong>7. datetime类型转date/转time</strong></h2><ul>
<li><p>转date:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">CONVERT(varchar(10),getdate(),120)</span><br></pre></td></tr></table></figure>
</li>
<li><p>转time:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">CONVERT(varchar(12),getdate(),108)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>具体的数字对应的时间格式见<a href="http://www.w3school.com.cn/sql/func_convert.asp" target="_blank" rel="external">4</a></p>
<h2 id="8-清空表"><a href="#8-清空表" class="headerlink" title="8. 清空表"></a><strong>8. 清空表</strong></h2><p>TRUNCATE TABLE 表名</p>
<h2 id="9-表名是变量形式"><a href="#9-表名是变量形式" class="headerlink" title="9. 表名是变量形式"></a><strong>9. 表名是变量形式</strong></h2><p>需要使用exec执行sql语句</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">declare</span> @<span class="keyword">table</span> <span class="built_in">varchar</span>(<span class="number">100</span>)</span><br><span class="line"><span class="keyword">declare</span> @<span class="keyword">sql</span> <span class="built_in">varchar</span>(<span class="number">100</span>)</span><br><span class="line"><span class="keyword">set</span> @<span class="keyword">table</span>=<span class="string">'abc'</span></span><br><span class="line"><span class="keyword">set</span> @<span class="keyword">sql</span>=<span class="string">'select * from '</span>+@<span class="keyword">table</span></span><br><span class="line">exec (@<span class="keyword">sql</span>)</span><br></pre></td></tr></table></figure>
<h2 id="10-exec里面除了表名还有其他变量"><a href="#10-exec里面除了表名还有其他变量" class="headerlink" title="10. exec里面除了表名还有其他变量"></a><strong>10. exec里面除了表名还有其他变量</strong></h2><p>这时需要用到sp_executesql来传递参数</p>
<p>示例:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">declare</span> @<span class="keyword">count</span> <span class="built_in">int</span>,@tableName <span class="keyword">nvarchar</span>(<span class="number">50</span>),@SQLString <span class="keyword">nvarchar</span>(<span class="keyword">max</span>),@proid <span class="built_in">int</span>,@<span class="keyword">id</span> <span class="built_in">int</span>,@ParmDefinition <span class="keyword">nvarchar</span>(<span class="keyword">max</span>);</span><br><span class="line"><span class="keyword">set</span> @tableName=<span class="string">'table27'</span>;</span><br><span class="line"><span class="keyword">set</span> @proid=<span class="number">433</span>;</span><br><span class="line"><span class="keyword">set</span> @<span class="keyword">id</span>=<span class="number">159</span>;</span><br><span class="line"><span class="comment">--set @sql=N'select @count=count(empid) from table27'</span></span><br><span class="line"><span class="keyword">set</span> @SQLString=N<span class="string">'select @countOUT=count(empid) from '</span>+@tableName+<span class="string">' where proid=@proid1 and id&lt;@id1 and state!=4'</span>;</span><br><span class="line"><span class="keyword">set</span> @ParmDefinition=N<span class="string">'@proid1 int,@id1 int,@countOUT int output'</span>;</span><br><span class="line">exec sp_executesql @SQLString,@ParmDefinition,@proid1=@proid,@id1=@id,@countOUT=@count output;</span><br><span class="line"><span class="keyword">select</span> @<span class="keyword">count</span>;</span><br></pre></td></tr></table></figure>
<p>【注】其中@sql字符串前面的‘N’为unicode编码,不加会报错‘过程需要类型为 ‘ntext/nchar/nvarchar’ 的参数 ‘@parameters’</p>
</content>
<summary type="html">
<p>数据终于处理完了,简直神清气爽,现在想想,整体思路还是比较简单清晰的,只是过程有点艰难,常常让我觉得得多吃点坚果补补脑。以下是整理的这几天解决的问题。</p>
</summary>
<category term="SQL" scheme="http://yoursite.com/tags/SQL/"/>
<category term="数据库" scheme="http://yoursite.com/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/"/>
<category term="数据处理" scheme="http://yoursite.com/tags/%E6%95%B0%E6%8D%AE%E5%A4%84%E7%90%86/"/>
</entry>
<entry>
<title>写在前面</title>
<link href="http://yoursite.com/2016/03/08/%E5%86%99%E5%9C%A8%E5%89%8D%E9%9D%A2/"/>
<id>http://yoursite.com/2016/03/08/写在前面/</id>
<published>2016-03-08T08:13:16.000Z</published>
<updated>2016-03-13T02:33:04.026Z</updated>
<content type="html"><p>欢迎来到Mia的新家,一直想给自己搭个博客,酝酿了好久,终于想记录的东西太多,才开始动手。</p>
<p>之前总觉得要学的东西太多,急匆匆的赶路,常常忽略总结和回顾。以后我会在这里和大家分享我的成长。喜欢Litten说的,这里更像是属于自己的一块小小的领地,因而我满足于这种归属感。我愿在上面安静劳作。</p>
<p>默默的许下一个愿望,在下一个秋天到的时候,能收货一片金黄。</p>
</content>
<summary type="html">
<p>欢迎来到Mia的新家,一直想给自己搭个博客,酝酿了好久,终于想记录的东西太多,才开始动手。</p>
<p>之前总觉得要学的东西太多,急匆匆的赶路,常常忽略总结和回顾。以后我会在这里和大家分享我的成长。喜欢Litten说的,这里更像是属于自己的一块小小的领地,因而我满足于这种
</summary>
<category term="杂谈" scheme="http://yoursite.com/tags/%E6%9D%82%E8%B0%88/"/>
</entry>
</feed>