-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
2257 lines (992 loc) · 116 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
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
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">
<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">
<link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">
<meta name="keywords" content="Hexo, NexT">
<meta property="og:type" content="website">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="https://niuzhaoxia.github.io/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Hexo">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Pisces',
version: '5.1.4',
sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
fancybox: true,
tabs: true,
motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
duoshuo: {
userId: '0',
author: '博主'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<link rel="canonical" href="https://niuzhaoxia.github.io/">
<title>Hexo</title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container sidebar-position-left
page-home">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">Hexo</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle"></p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br>
首页
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
标签
</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section">
<i class="menu-item-icon fa fa-fw fa-th"></i> <br>
分类
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
归档
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="https://niuzhaoxia.github.io/2019/07/31/jQuery总结/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="niuzhaoxia">
<meta itemprop="description" content>
<meta itemprop="image" content="/images/headPortrait.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Hexo">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/07/31/jQuery总结/" itemprop="url">'jQuery总结'</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2019-07-31T11:34:00+08:00">
2019-07-31
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h4 id="1、掌握jq常用的获取元素对象的方式以及常用的选择器"><a href="#1、掌握jq常用的获取元素对象的方式以及常用的选择器" class="headerlink" title="1、掌握jq常用的获取元素对象的方式以及常用的选择器"></a>1、掌握jq常用的获取元素对象的方式以及常用的选择器</h4><blockquote>
<p>jquery通过选择器的方式获取的元素,不一定只获取一个,而是不管获取到几个都会返回一个元素集合(类数组),这些元素在类数组中获得了一个索引值。原生方法获取不到时会返回null,jq获取不到仍然返回一个对象。在jq的返回对象中有一个length属性,这个属性表示获取到的个数。</p>
</blockquote>
<ol>
<li>基本选择器</li>
</ol>
<ul>
<li>id 选择器 #ID</li>
<li>className 类选择器 .className</li>
<li>标签选择器 p ul li div<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="built_in">console</span>.log(<span class="string">'id 选择器'</span>, $(<span class="string">'#tabBox'</span>));</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'className 选择器'</span>, $(<span class="string">'.header'</span>));</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'标签选择器 '</span>, $(<span class="string">'li'</span>));</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'原生方法获取不到时 '</span>, <span class="built_in">document</span>.getElementById(<span class="string">'zhangsan'</span>));</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'jq 获取不到时'</span>, $(<span class="string">'#tabBox>li'</span>));</span><br></pre></td></tr></table></figure>
</li>
</ul>
<ol start="2">
<li>层级选择器<ul>
<li>后代选择器 就是一个空格 #tabBox li (获取id为tabBox的所有子孙代中的li);</li>
<li>子级选择器 > #tabBox > ul (只在儿子代中查找)</li>
<li>兄弟选择器 + .header + div </li>
<li>获取所有的弟弟 li.active ~ </li>
</ul>
</li>
</ol>
<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="built_in">console</span>.log($(<span class="string">'#tabBox li'</span>));</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'.header + div'</span>));</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'li.active ~'</span>));</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>过滤选择器<ul>
<li>:first 获取第一个</li>
<li>:last 获取最后一个</li>
<li>:odd 获取奇数索引的</li>
<li>:even 获取偶数索引的</li>
<li>:not(selector) 除了selector的都要; :not(first) 除了第一个都要</li>
<li>:eq(index) 获取索引为index的</li>
<li>:gt(index) 获取比索引index大的</li>
<li>:lt(index) 获取比索引index小的</li>
</ul>
</li>
</ol>
<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="built_in">console</span>.log($(<span class="string">'.header li:first'</span>));</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'.header li:last'</span>));</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'.header li:odd'</span>));</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'.header li:even'</span>));</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'.header li:not(:first)'</span>));</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'.header li:eq(1)'</span>));</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'.header li:gt(1)'</span>));</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'.header li:lt(1)'</span>));</span><br></pre></td></tr></table></figure>
<ol start="4">
<li>内容选择器<ul>
<li>:contains(‘内容’) 获取包含指定内容的元素</li>
<li>:empty 获取空标签元素</li>
<li>:has(selector) 获取包含指定选择器元素的元素</li>
<li>:hidden 获取不可见的元素对象</li>
</ul>
</li>
</ol>
<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="built_in">console</span>.log($(<span class="string">'p:contains("霸道")'</span>));</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'p:empty'</span>));</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'p:has("span")'</span>));</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'p:hidden'</span>));</span><br></pre></td></tr></table></figure>
<ol start="5">
<li>属性选择器<ul>
<li>[attr=val] 获取含有属性attr并且其值是val的元素</li>
<li>[attr^=val] 获取含有attr属性并且其值是以val开头的元素</li>
<li>[attr$=val] 获取含有attr属性并且其值是以val结尾的元素</li>
<li>5.4 [attr*=val] 获取含有attr属性并且其值包含val的元素</li>
</ul>
</li>
</ol>
<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="built_in">console</span>.log($(<span class="string">'input[name=man]'</span>));</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'input[name^=man]'</span>));</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'input[name$=man]'</span>));</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'input[name*=man]'</span>));</span><br></pre></td></tr></table></figure>
<h4 id="2、jq对象和原生元素对象互转"><a href="#2、jq对象和原生元素对象互转" class="headerlink" title="2、jq对象和原生元素对象互转"></a>2、jq对象和原生元素对象互转</h4><blockquote>
<p><code>jq对象转原生对象</code>:jq对象中索引对应的就是原生元素对象,我们可以通过[索引]就能获取到这个原生对象,此外还有两种方法可以获取这个原生对象</p>
</blockquote>
<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="comment">// jq对象.get(index) index 是你想获取的原生对象的索引</span></span><br><span class="line"><span class="comment">// let $ul = $('#emptyUl');</span></span><br><span class="line"><span class="comment">// console.log($ul[0]);</span></span><br><span class="line"><span class="comment">// console.log($ul.get(0));</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><code>原生对象转jq对象</code>:将我们通过原生方法获取到原生对象当做参数传给<strong>$方法($(原生对象))</strong>就可以把原生对象转成jq对象。</p>
</blockquote>
<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">let</span> ul = <span class="built_in">document</span>.getElementById(<span class="string">'emptyUl'</span>);</span><br><span class="line"><span class="keyword">let</span> $ul = $(ul); <span class="comment">// 把ul原生对象当做实参传给 $() 方法</span></span><br><span class="line"><span class="built_in">console</span>.log($ul);</span><br><span class="line">$ul.html(<span class="string">'<li>转换之后就是爽</li>'</span>);</span><br><span class="line">ul.html(<span class="string">'<li>123456789</li>'</span>);</span><br></pre></td></tr></table></figure>
<h4 id="3、jq常用的方法"><a href="#3、jq常用的方法" class="headerlink" title="3、jq常用的方法"></a>3、jq常用的方法</h4><blockquote>
<p>以 <strong>let $tabBox = $(‘#tabBox’)</strong> 为例</p>
</blockquote>
<ol>
<li><code>height()</code>: 不传参数是获取元素对象的高度,传参就是修改元素对象的高度</li>
</ol>
<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="built_in">console</span>.log($tabBox.height());</span><br><span class="line">$tabBox.height(<span class="number">200</span> + <span class="string">'rem'</span>); </span><br><span class="line"><span class="comment">// 默认单位是px,如果需要其他单位,就需要显示的设置,如设置rem(一个单位)</span></span><br></pre></td></tr></table></figure>
<ol start="2">
<li><code>width()</code>: 获取元素对象的宽度,传递参数是设置元素对象的宽度</li>
</ol>
<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="built_in">console</span>.log($tabBox.width());</span><br></pre></td></tr></table></figure>
<ol start="3">
<li><code>offset</code> 获取元素到body左侧和上侧的偏移距离,返回值是一个对象{left: ,top: }</li>
</ol>
<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="built_in">console</span>.log($tabBox.offset());</span><br></pre></td></tr></table></figure>
<ol start="4">
<li><code>scrollTop() / scrollLeft()</code> 获取纵向 / 横向滚动条卷去的距离<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="built_in">console</span>.log($(<span class="built_in">document</span>.documentElement).scrollTop());</span><br></pre></td></tr></table></figure>
</li>
</ol>
<ol start="5">
<li><code>innerHeight() / innerWidth()</code>获取当前元素的高度 / 宽度 等效于</li>
</ol>
<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">clientHeight / clientWidth</span><br><span class="line"><span class="built_in">console</span>.log($tabBox.innerHeight());</span><br><span class="line"><span class="built_in">console</span>.log($tabBox.innerWidth());</span><br></pre></td></tr></table></figure>
<ol start="6">
<li><code>outerHeight() / outerWidth()</code> 获取元素包含边框的高度/宽度 相当于 offsetHeight / offsetWidth</li>
</ol>
<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="built_in">console</span>.log($tabBox.outerHeight());</span><br></pre></td></tr></table></figure>
<ol start="7">
<li><code>hasClass(类名)</code> 判断当前元素有没有这个类名,有就返回true否则返回false</li>
</ol>
<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="built_in">console</span>.log($tabBox.hasClass(<span class="string">'tabBox1'</span>));</span><br></pre></td></tr></table></figure>
<ol start="8">
<li><code>addClass(类名)</code>为当前元素追加类名,返回当前jq元素对象(return this)</li>
</ol>
<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="built_in">console</span>.log($tabBox.addClass(<span class="string">'classByJq'</span>));</span><br></pre></td></tr></table></figure>
<ol start="9">
<li><code>removeClass(类名)</code> 移除当前元素的某一个类名,返回当前jq元素对象</li>
</ol>
<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="built_in">console</span>.log($tabBox.removeClass(<span class="string">'classByJq'</span>));</span><br></pre></td></tr></table></figure>
<ol start="10">
<li><code>append()</code> 向元素对象内追加元素</li>
</ol>
<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">let</span> $emptyUl = $(<span class="string">'#emptyUl'</span>);</span><br><span class="line">$emptyUl.append($(<span class="string">'<li>12345678</li>'</span>));</span><br></pre></td></tr></table></figure>
<ol start="11">
<li><code>元素.appendTo(容器)</code>将当前插入到指定容器中</li>
</ol>
<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="string">'<li>aaaaaaaaaaa</li>'</span>).appendTo($emptyUl);</span><br></pre></td></tr></table></figure>
<ol start="12">
<li><code>prepend() / prependTo()</code> 向容器开头添加元素对象</li>
</ol>
<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">$emptyUl.prepend($(<span class="string">'<li>bbbbbbbbbb</li>'</span>));</span><br><span class="line">$(<span class="string">'<li>ccccccccc</li>'</span>).prependTo($emptyUl);</span><br></pre></td></tr></table></figure>
<ol start="13">
<li><code>before() / after()</code> 给当前元素之前或者之后添加【兄弟】元素<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">$emptyUl.before($(<span class="string">'<li>ddddddddd</li>'</span>));</span><br></pre></td></tr></table></figure>
</li>
</ol>
<ol start="14">
<li><p><code>insertBefore() / insertAfter</code> 将当前元素添加到参照元素之前或者之后</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">let</span> newObj = $(<span class="string">'<div>wwwwww</div>'</span>); </span><br><span class="line"><span class="comment">// 使用jq创建一个新元素,无需createElement。</span></span><br><span class="line">newObj.insertBefore($emptyUl);</span><br><span class="line">newObj.insertAfter($emptyUl);</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>replaceAll(selector)</code> 用当前元素替换selector匹配到元素</p>
</li>
</ol>
<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="string">'<ol></ol>'</span>).replaceAll(<span class="string">'ul'</span>);</span><br></pre></td></tr></table></figure>
<ol start="16">
<li><p><code>元素.remove()</code> 移除元素</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">$emptyUl.remove();</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>next() / nextAll()</code> 获取当前元素的下一个弟弟 / 获取所有的弟弟</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">console</span>.log($emptyUl.next());</span><br><span class="line"><span class="built_in">console</span>.log($emptyUl.nextAll());</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>prev() / prevAll()</code>获取当前元素的上一个哥哥 / 获取当前元素所有的哥哥</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">console</span>.log($(<span class="string">'#contentFilter'</span>).prev());</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'#contentFilter'</span>).prevAll());</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>children()</code> 获取当前元素的元素儿子</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">console</span>.log($(<span class="string">'#contentFilter'</span>).children()); <span class="comment">// 获取所有的儿子</span></span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'#contentFilter'</span>).children(<span class="string">'.first'</span>)); <span class="comment">// 获取有first类名的儿子(children() 方法还可以使用其他选择器过滤)</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><code>parent()</code> 获取当前元素的元素父亲</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="built_in">console</span>.log($(<span class="string">'.first'</span>).parent());</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>filter(selector)</code> 在同级中把满足selector的元素获取到(同级查找,在兄弟之间查找)</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">console</span>.log($(<span class="string">'.header li'</span>).filter(<span class="string">'.active'</span>));</span><br><span class="line"><span class="comment">// 把header下的li并且是带有active类名的li获取到</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><code>find(selector)</code> 从当前元素的后代中获取满足selector的元素</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="built_in">console</span>.log($(<span class="string">'#tabBox'</span>).find(<span class="string">'li.active'</span>));</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong><code>css 方法</code></strong>,</p>
</li>
</ol>
<ul>
<li>元素.css(‘width’) 获取元素的width</li>
<li>元素.css(‘width’, ‘200px’) 设置元素的width</li>
<li>元素.css({width: ‘200px’, backgroundColor: ‘#00b38a’})</li>
<li>$(‘#tabBox’).css(‘height’, 250); // 不写单位,默认单位是px</li>
<li>console.log($(‘#tabBox’).css(‘width’)); // 返回了一个字符串,后面就不能链式调用(链式调用也叫级联)</li>
<li>console.log($(‘#tabBox’).css(‘width’, 200)); // 返回了jq对象,可以继续链式调用</li>
</ul>
<ol start="24">
<li><p><code>text()</code> 传参就是设置元素的innerText,不传是获取元素的innerText</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="built_in">console</span>.log($(<span class="string">'#tabBox'</span>).text());</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'li.active'</span>).text());</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'li.active'</span>).text(<span class="string">'嘻哈'</span>));</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>html()</code> 传参就是设置元素的innerHTML,不传参就是获取innerHTML</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">console</span>.log($(<span class="string">'#tabBox'</span>).html());</span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">'#tabBox'</span>).html(<span class="string">'<p>这是一个p</p>'</span>));</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h4 id="4、jq绑定事件和移除事件的方法"><a href="#4、jq绑定事件和移除事件的方法" class="headerlink" title="4、jq绑定事件和移除事件的方法"></a>4、jq绑定事件和移除事件的方法</h4><blockquote>
<p>1、 将原生事件名中on去掉,例如onclick,jq提供了一个对应事件名的方法,click() ,要把事件触发时的执行的事件函数作为参数传给click方法。</p>
</blockquote>
<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">btn.onclick = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> alert(<span class="string">'来啊,快活啊'</span>)</span><br><span class="line">};</span><br><span class="line"><span class="comment">// 以上为原生方法</span></span><br><span class="line">$(btn).click(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> alert(<span class="string">'来啊,快活啊'</span>)</span><br><span class="line">});</span><br><span class="line"><span class="keyword">let</span> fun = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> alert(<span class="string">'欢迎下次常来'</span>);</span><br><span class="line">};</span><br><span class="line">$(btn).click(fun);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>2、 on() 方法:语法:jq对象.on(事件名[不带on], 事件函数);</p>
</blockquote>
<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">$(btn).on(<span class="string">'click'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">'这个是on方法绑定的'</span>);</span><br><span class="line">});</span><br></pre></td></tr></table></figure>
<blockquote>
<p>3、 trigger() 用代码触发事件 参数就是不带on的事件名 ;返回值是当前jq对象,可以继续链式调用</p>
</blockquote>
<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="string">'#btn'</span>).trigger(<span class="string">'click'</span>);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>4、off() 移除事件,参数是不带on的事件名,返回值是当前jq对象,可以继续链式调用</p>
</blockquote>
<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="string">'#btn'</span>).off(<span class="string">'click'</span>);</span><br></pre></td></tr></table></figure>
<h4 id="5、jq简单动画效果"><a href="#5、jq简单动画效果" class="headerlink" title="5、jq简单动画效果"></a>5、jq简单动画效果</h4><blockquote>
<p><code>hide() / show()</code> 隐藏元素 / 显示元素 参数是完成动画时间(ms) ,还可以写 ‘slow’慢(600ms), ‘fast’ 快(200ms)</p>
</blockquote>
<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="string">'#bt1'</span>).click(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> $(<span class="string">'.a-obj'</span>).hide(<span class="string">'fast'</span>);</span><br><span class="line">});</span><br><span class="line">$(<span class="string">'#bt2'</span>).on(<span class="string">'click'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> $(<span class="string">'.a-obj'</span>).show(<span class="number">3000</span>);</span><br><span class="line">});</span><br></pre></td></tr></table></figure>
<blockquote>
<p><code>slideUp() / slideDown()</code> 向上滑动收起 / 向下滑动展开</p>
</blockquote>
<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="string">'#bt1'</span>).click(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> $(<span class="string">'.a-obj'</span>).slideUp();</span><br><span class="line">});</span><br><span class="line">$(<span class="string">'#bt2'</span>).on(<span class="string">'click'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> $(<span class="string">'.a-obj'</span>).slideDown();</span><br><span class="line">});</span><br></pre></td></tr></table></figure>
<blockquote>
<p><code>fadeIn() / fadeOut()</code> 淡入/淡出</p>
</blockquote>
<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="string">'#bt1'</span>).click(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> $(<span class="string">'.a-obj'</span>).fadeOut();</span><br><span class="line">});</span><br><span class="line">$(<span class="string">'#bt2'</span>).on(<span class="string">'click'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> $(<span class="string">'.a-obj'</span>).fadeIn();</span><br><span class="line">});</span><br></pre></td></tr></table></figure>
<blockquote>
<p><code>slideToggle()</code> 第一次如果是收起的,执行slideToggle就是展开,如果上一次是展开的,我们执行slideToggle就是收起,适合做点击展开,再点击收起的需求</p>
</blockquote>
<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="string">'#bt3'</span>).on(<span class="string">'click'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> $(<span class="string">'.a-obj'</span>).slideToggle();</span><br><span class="line">});</span><br></pre></td></tr></table></figure>
<blockquote>
<p>animate() 语法:<code>$(selector).animate(target, duration, easing, afterCallback)</code></p>
</blockquote>
<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="string">'.a-obj'</span>).animate({</span><br><span class="line"> left: <span class="number">300</span></span><br><span class="line">}, <span class="number">4000</span>);</span><br></pre></td></tr></table></figure>
<blockquote>
<p><code>stop()</code> 停止当前元素正在进行的动画,不管到没到终点都得停;<br><code>finish()</code> 停止当前元素正在进行的动画,但是它是忽略动画规定的时间,一下子到终点。</p>
</blockquote>
<h4 id="6、jq-ajax的使用方法、配置参数及意义"><a href="#6、jq-ajax的使用方法、配置参数及意义" class="headerlink" title="6、jq ajax的使用方法、配置参数及意义"></a>6、jq ajax的使用方法、配置参数及意义</h4><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></pre></td><td class="code"><pre><span class="line">$.ajax({</span><br><span class="line"> url: <span class="string">'json/data.json'</span>, <span class="comment">// 接口地址</span></span><br><span class="line"> method: <span class="string">'GET'</span>, <span class="comment">// 请求方式</span></span><br><span class="line"> <span class="keyword">async</span>: <span class="literal">false</span>, <span class="comment">// 是否异步</span></span><br><span class="line"> dataType: <span class="string">'json'</span>, <span class="comment">// 数据类型 一般为json,也可以设置其他如xml</span></span><br><span class="line"> success: <span class="function"><span class="keyword">function</span> (<span class="params">data</span>) </span>{</span><br><span class="line"> <span class="built_in">console</span>.log(data)</span><br><span class="line"> },</span><br><span class="line"> error: <span class="function"><span class="keyword">function</span>(<span class="params">err</span>) </span>{</span><br><span class="line"> <span class="built_in">console</span>.log(err)</span><br><span class="line"> },</span><br><span class="line"> cache: <span class="literal">false</span> <span class="comment">// 是否设置缓存 一般不缓存</span></span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<h4 id="7、jq-的each方法遍历常用的数据结构"><a href="#7、jq-的each方法遍历常用的数据结构" class="headerlink" title="7、jq 的each方法遍历常用的数据结构"></a>7、jq 的each方法遍历常用的数据结构</h4><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="comment">// 遍历数组</span></span><br><span class="line">$.each([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], <span class="function"><span class="keyword">function</span> (<span class="params">index, item</span>) </span>{</span><br><span class="line"> <span class="built_in">console</span>.log(index, item);</span><br><span class="line">});</span><br><span class="line"><span class="comment">// 遍历对象</span></span><br><span class="line">$.each({<span class="attr">name</span>: <span class="string">'zf'</span>, <span class="attr">age</span>: <span class="number">11</span>}, <span class="function"><span class="keyword">function</span> (<span class="params">key, val</span>) </span>{</span><br><span class="line"> <span class="built_in">console</span>.log(key, val);</span><br><span class="line">});</span><br></pre></td></tr></table></figure>
<blockquote>
<p>jq内部隐式调用each</p>
</blockquote>
<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="string">'.header > li'</span>).click(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> <span class="comment">// jq内部如果发现你获取到是一个元素集合,它会隐式调用each方法循环完成事件绑定。</span></span><br><span class="line"> <span class="comment">// jq事件函数中的this指向绑定该事件的元素。</span></span><br><span class="line"> <span class="built_in">console</span>.log(<span class="keyword">this</span>);</span><br><span class="line">});</span><br></pre></td></tr></table></figure>
<h4 id="8、向jq自身上扩展方法-amp-向jq原型上添加方法"><a href="#8、向jq自身上扩展方法-amp-向jq原型上添加方法" class="headerlink" title="8、向jq自身上扩展方法&向jq原型上添加方法"></a>8、向jq自身上扩展方法&向jq原型上添加方法</h4><ol>
<li>向jq自身上扩展方法: $.extend({方法名:方法对应的函数, ….})</li>
</ol>
<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">$.extend({</span><br><span class="line"> sayName: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">'I am bingo'</span>);</span><br><span class="line"> }</span><br><span class="line">});</span><br><span class="line">$.sayName();</span><br><span class="line"><span class="comment">// $('#fields').sayName(); // 报错,因为方法是定义jq自身的,只能jq自己调用</span></span><br></pre></td></tr></table></figure>
<ol start="2">
<li>向jq的原型上扩展方法:$.fn.extend({方法名:function () {}, …})</li>
</ol>
<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">$.fn.extend({</span><br><span class="line"> hello: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line"> <span class="built_in">console</span>.log(<span class="string">'hello world'</span>);</span><br><span class="line"> }</span><br><span class="line">});</span><br><span class="line"></span><br><span class="line">$(<span class="string">'#fields'</span>).hello();</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="https://niuzhaoxia.github.io/2019/07/09/js的n种继承方式/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="niuzhaoxia">
<meta itemprop="description" content>
<meta itemprop="image" content="/images/headPortrait.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Hexo">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/07/09/js的n种继承方式/" itemprop="url">js的n种继承方式</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2019-07-09T19:13:02+08:00">
2019-07-09
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="https://niuzhaoxia.github.io/2019/07/09/http-tcp知识整理/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="niuzhaoxia">
<meta itemprop="description" content>
<meta itemprop="image" content="/images/headPortrait.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Hexo">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/07/09/http-tcp知识整理/" itemprop="url">http/tcp知识整理</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2019-07-09T10:44:17+08:00">
2019-07-09
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="HTTP"><a href="#HTTP" class="headerlink" title="HTTP"></a>HTTP</h2><h3 id="HTTP介绍"><a href="#HTTP介绍" class="headerlink" title="HTTP介绍"></a>HTTP介绍</h3><p>HTTP协议(HyperText Transfer Protocol,超文本传输协议)是因特网上应用最为广泛的一种网络传输协议,所有的WWW文件都必须遵守这个标准。它是是一个基于TCP/IP通信协议来传递数据(HTML 文件, 图片文件, 查询结果等)。</p>
<p>HTTP协议工作于客户端-服务端架构上。浏览器作为HTTP客户端通过URL向HTTP服务端即WEB服务器发送所有请求。</p>
<h3 id="HTTP消息结构"><a href="#HTTP消息结构" class="headerlink" title="HTTP消息结构"></a>HTTP消息结构</h3><p>HTTP是基于客户端/服务端(C/S)的架构模型,通过一个可靠的链接来交换信息,是一个无状态的请求/响应协议。<br>一个HTTP”客户端”是一个应用程序(Web浏览器或其他任何客户端),通过连接到服务器达到向服务器发送一个或多个HTTP的请求的目的。<br>一个HTTP”服务器”同样也是一个应用程序(通常是一个Web服务,如Apache Web服务器或IIS服务器等),通过接收客户端的请求并向客户端发送HTTP响应数据。<br>HTTP使用统一资源标识符(Uniform Resource Identifiers, URI)来传输数据和建立连接。<br>一旦建立连接后,数据消息就通过类似Internet邮件所使用的格式[RFC5322]和多用途Internet邮件扩展(MIME)[RFC2045]来传送。</p>
<h3 id="HTTP传输流"><a href="#HTTP传输流" class="headerlink" title="HTTP传输流"></a>HTTP传输流</h3><img src="https://user-gold-cdn.xitu.io/2018/4/17/162d19df66466f6b?imageslim">
<h3 id="请求报文"><a href="#请求报文" class="headerlink" title="请求报文"></a>请求报文</h3><p>客户端发送一个HTTP请求到服务器的请求消息包括以下格式:请求行(request line)、请求头部(header)、空行和请求数据四个部分组成</p>
<img src="/images/报文.png" class="[class names]">
<h3 id="HTTP请求方法"><a href="#HTTP请求方法" class="headerlink" title="HTTP请求方法"></a>HTTP请求方法</h3><p>根据 HTTP 标准,HTTP 请求可以使用多种请求方法。</p>
<p>HTTP1.0 定义了三种请求方法: GET, POST 和 HEAD方法。<br>HTTP1.1 新增了五种请求方法:OPTIONS、PUT、DELETE、TRACE 和 CONNECT 方法。</p>
<ul>
<li>OPTIONS 允许客户端查看服务器的性能。</li>
<li>PUT 从客户端向服务器传送的数据取代指定的文档的内容。</li>
<li>DELETE 请求服务器删除指定的页面。</li>
<li>TRACE 回显服务器收到的请求,主要用于测试或诊断。</li>
<li>CONNECT HTTP/1.1 协议中预留给能够将连接改为管道方式的代理服务器。</li>
</ul>
<h3 id="HTTP常用的状态码"><a href="#HTTP常用的状态码" class="headerlink" title="HTTP常用的状态码"></a>HTTP常用的状态码</h3><blockquote>
<p>2XX 成功</p>
</blockquote>
<ul>
<li>200 OK,表示从客户端发来的请求在服务器端被正确处理</li>
<li>204 No content,表示请求成功,但响应报文不含实体的主体部分</li>
<li>206 Partial Content,进行范围请求</li>
</ul>
<blockquote>
<p>3XX 重定向</p>
</blockquote>
<ul>
<li>301 moved permanently,永久性重定向,表示资源已被分配了新的 URL</li>
<li>302 found,临时性重定向,表示资源临时被分配了新的 URL</li>
<li>303 see other,表示资源存在着另一个 URL,应使用 GET 方法丁香获取资源</li>
<li>304 not modified,表示服务器允许访问资源,但因发生请求未满足条件的情况</li>
<li>307 temporary redirect,临时重定向,和302含义相同</li>
</ul>
<blockquote>
<p>4XX 客户端错误</p>
</blockquote>
<ul>
<li>400 bad request,请求报文存在语法错误</li>
<li>401 unauthorized,表示发送的请求需要有通过 HTTP 认证的认证信息</li>
<li>403 forbidden,表示对请求资源的访问被服务器拒绝</li>
<li>404 not found,表示在服务器上没有找到请求的资源</li>
</ul>
<blockquote>
<p>5XX 服务器错误</p>
</blockquote>
<ul>
<li>500 internal sever error,表示服务器端在执行请求时发生了错误</li>
<li>503 service unavailable,表明服务器暂时处于超负载或正在停机维护,无法处理请求</li>
</ul>
<h4 id="304-浏览器缓存机制"><a href="#304-浏览器缓存机制" class="headerlink" title="304 浏览器缓存机制"></a>304 浏览器缓存机制</h4><img src="/images/http-cache.jpg">
<blockquote><p>缓存过程 -> 强制缓存和协商缓存</p>
<footer><strong>HeyingYe</strong><cite><a href="https://heyingye.github.io/2018/04/16/%E5%BD%BB%E5%BA%95%E7%90%86%E8%A7%A3%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E7%BC%93%E5%AD%98%E6%9C%BA%E5%88%B6/" target="_blank" rel="noopener">彻底理解浏览器的缓存机制</a></cite></footer></blockquote>
<h3 id="HTTP三点注意事项:"><a href="#HTTP三点注意事项:" class="headerlink" title="HTTP三点注意事项:"></a>HTTP三点注意事项:</h3><ul>
<li>HTTP是<code>无连接</code>:无连接的含义是限制每次连接只处理一个请求。服务器处理完客户的请求,并收到客户的应答后,即断开连接。采用这种方式可以节省传输时间。</li>
<li>HTTP是<code>媒体独立</code>的:这意味着,只要客户端和服务器知道如何处理的数据内容,任何类型的数据都可以通过HTTP发送。客户端以及服务器指定使用适合的MIME-type内容类型。</li>
<li>HTTP是<code>无状态</code>:HTTP协议是无状态协议。无状态是指协议对于事务处理没有记忆能力。缺少状态意味着如果后续处理需要前面的信息,则它必须重传,这样可能导致每次连接传送的数据量增大。另一方面,在服务器不需要先前信息时它的应答就较快。</li>
</ul>
<h2 id="TCP"><a href="#TCP" class="headerlink" title="TCP"></a>TCP</h2><h3 id="三次握手"><a href="#三次握手" class="headerlink" title="三次握手"></a>三次握手</h3><ul>
<li>客户端–发送带有SYN标志的数据包–一次握手–服务端</li>
<li>服务端–发送带有SYN/ACK标志的数据包–二次握手–客户端</li>
<li>客户端–发送带有带有ACK标志的数据包–三次握手–服务端</li>
</ul>
<img src="/images/三次握手.png">
<h3 id="四次挥手"><a href="#四次挥手" class="headerlink" title="四次挥手"></a>四次挥手</h3><ul>
<li>客户端-发送一个FIN,用来关闭客户端到服务器的数据传送</li>
<li>服务器-收到这个FIN,它发回一个ACK,确认序号为收到的序号加1 。和SYN一样,一个FIN将占用一个序号</li>
<li>服务器-关闭与客户端的连接,发送一个FIN给客户端</li>
<li>客户端-发回ACK报文确认,并将确认序号设置为收到序号加1</li>
</ul>
<img src="/images/四次挥手.png">
<h3 id="TCP与UDP的区别:"><a href="#TCP与UDP的区别:" class="headerlink" title="TCP与UDP的区别:"></a>TCP与UDP的区别:</h3><ul>
<li>基于连接与无连接;</li>
<li>对系统资源的要求(TCP较多,UDP少);</li>
<li>UDP程序结构较简单;</li>
<li>流模式与数据报模式 ;</li>
<li>TCP保证数据正确性,UDP可能丢包;</li>
<li>TCP保证数据顺序,UDP不保证。</li>
</ul>
<h2 id="文章参考:"><a href="#文章参考:" class="headerlink" title="文章参考:"></a>文章参考:</h2><blockquote><footer><strong>白伟业</strong><cite><a href="https://juejin.im/post/5ad4094e6fb9a028d7011069" target="_blank" rel="noopener">tcp、http面试指南</a></cite></footer></blockquote>
<blockquote><footer><strong>寻找海蓝96</strong><cite><a href="https://juejin.im/post/5d032b77e51d45777a126183" target="_blank" rel="noopener">http面试</a></cite></footer></blockquote>
<blockquote><footer><strong>github</strong><cite><a href="https://hit-alibaba.github.io/interview/basic/network/TCP.html" target="_blank" rel="noopener">tcp协议</a></cite></footer></blockquote>
<blockquote><footer><strong>陈宝佳</strong><cite><a href="https://zhuanlan.zhihu.com/p/24860273" target="_blank" rel="noopener">TCP和UDP的区别</a></cite></footer></blockquote>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="https://niuzhaoxia.github.io/2019/07/06/解决跨域问题的几种方法/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="niuzhaoxia">
<meta itemprop="description" content>
<meta itemprop="image" content="/images/headPortrait.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Hexo">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/07/06/解决跨域问题的几种方法/" itemprop="url">解决跨域问题的几种方法</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2019-07-06T18:14:28+08:00">
2019-07-06
</time>
</span>