Web前端概述.md 15.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
## Web前端概述

### HTML简史

1. 1991年10月:一个非正式CERN([欧洲核子研究中心](https://zh.wikipedia.org/wiki/%E6%AD%90%E6%B4%B2%E6%A0%B8%E5%AD%90%E7%A0%94%E7%A9%B6%E7%B5%84%E7%B9%94))文件首次公开18个HTML标签,这个文件的作者是物理学家[蒂姆·伯纳斯-李](https://zh.wikipedia.org/wiki/%E8%92%82%E5%A7%86%C2%B7%E4%BC%AF%E7%BA%B3%E6%96%AF-%E6%9D%8E),因此他是[万维网](https://zh.wikipedia.org/wiki/%E4%B8%87%E7%BB%B4%E7%BD%91)的发明者,也是[万维网联盟](https://zh.wikipedia.org/wiki/%E4%B8%87%E7%BB%B4%E7%BD%91%E8%81%94%E7%9B%9F)的主席。
2. 1995年11月:HTML 2.0标准发布(RFC 1866)。
3. 1997年1月:HTML 3.2作为[W3C](https://zh.wikipedia.org/wiki/W3C)推荐标准发布。
4. 1997年12月:HTML 4.0作为W3C推荐标准发布。
5.  1999年12月:HTML4.01作为W3C推荐标准发布。
6. 2008年1月:HTML5由W3C作为工作草案发布。
7. 2011年5月:W3C将HTML5推进至“最终征求”(Last Call)阶段。
8. 2012年12月:W3C指定HTML5作为“候选推荐”阶段。
9. 2014年10月:HTML5作为稳定W3C推荐标准发布,这意味着HTML5的标准化已经完成。

#### HTML5新特性

1. 引入原生多媒体支持(audio和video标签)
2. 引入可编程内容(canvas标签)
3. 引入语义Web(article、aside、details、figure、footer、header、nav、section、summary等标签)
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
20 21
4. 引入新的表单控件(日历、邮箱、搜索、滑条等)
5. 引入对离线存储更好的支持(localStorage和sessionStorage)
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
6. 引入对定位、拖放、WebSocket、后台任务等的支持

### 使用标签承载内容

####  结构

- head
  - title
  - meta
- body

#### 文本

- 标题和段落
- 粗体和斜体
- 上标和下标
- 空白(白色空间折叠)
- 折行和水平标尺
- 语义化标记
  - 加粗和强调
  - 引用
  - 缩写词和首字母缩写词
  - 引文
  - 所有者联系信息
  - 内容的修改

#### 列表(list)

 - 有序列表(ordered list)
 - 无序列表(unordered list)
 - 定义列表(definition list)

#### 链接(anchor)

- 页面链接
- 锚链接
- 功能链接

#### 图像(image)

- 图像存储位置
- 图像及其宽高
- 选择正确的图像格式
  - JPEG
  - GIF
  - PNG
- 矢量图
- figure标签

#### 表格(table)

- 基本的表格结构
- 表格的标题
- 跨行和跨列
- 长表格

#### 表单(form)

- 如何收集信息
- 表单控件(input)
  - 文本框 / 密码框 / 文本域
  - 单选按钮 / 复选按钮 / 下拉列表
  - 提交按钮 / 图像按钮 / 文件上传
- 组合表单元素
  - fieldset / legend
- HTML5的表单控件
  - 日期
  - 电子邮件 / URL
  - 搜索

#### 音视频(audio / video)

- 视频格式和播放器
- 视频托管服务
- 添加视频的准备工作
- video标签和属性
- audio标签和属性

#### 其他

- 文档类型
- 注释
- 属性
  - id
  - class
- 块级元素 / 行级元素
- 内联框架(internal frame)
- 页面信息(meta)
- 转义字符(实体替换符)

### 使用CSS渲染页面

#### 简介

- CSS的作用
- CSS的工作原理
- 规则、属性和值

#### 颜色(color)

- 如何指定颜色
- 颜色术语和颜色对比
- 背景色

#### 文本(text / font)

- 文本的大小和字型(font-size / font-family)
- 斜体、粗体、大写和下划线(font-weight / font-style / text-decoration)
- 行间距(line-height)、字母间距(letter-spacing)和单词间距(word-spacing)
- 对齐(text-align)方式和缩进(text-ident)
- 链接样式(:link / :visited / :active / :hover)
- CSS3新属性
  - 投影
  - 首字母和首行文本(p:first-letter / p:first-line)
  - 响应用户

#### 盒子(box model)

- 盒子大小的控制(width / height)
- 盒子的边框、外边距和内边距(border /  margin / padding)
- 盒子的显示和隐藏(display / visibility)
- CSS3新属性
  - 边框图像(border-image)
  - 投影(border-shadow)
  - 圆角(border-radius)

#### 列表、表格和表单

- 列表的项目符号(list-style)
- 表格的边框和背景(border-collapse)
- 表单控件的外观
- 表单控件的对齐
- 浏览器的开发者工具

#### 图像

- 控制图像的大小(display: inline-block)
- 对齐图像
- 背景图像(background / background-image / background-repeat / background-position)

#### 布局

- 控制元素的位置(position / z-index)
  - 普通流
  - 相对定位
  - 绝对定位
  - 固定定位
  - 浮动元素(float / clear)
- 网站布局
  - HTML5布局
- 适配屏幕尺寸
  - 固定宽度布局
  - 流体布局
  - 布局网格

### 使用JavaScript控制行为

#### JavaScript基本语法

- 语句和注释
- 变量和数据类型
  - 声明和赋值
  - 简单数据类型和复杂数据类型
  - 变量的命名规则
- 表达式和运算符
  - 赋值运算符
  - 算术运算符
  - 比较运算符
  - 逻辑运算符
- 分支结构
  - if…else...
  - switch…case…default...
- 循环结构
  - for循环
  - while循环
  - do…while循环
- 数组
  - 创建数组
  - 操作数组中的元素
- 函数
  - 声明函数
  - 调用函数
  - 参数和返回值
  - 匿名函数
  - 立即调用函数

#### 面向对象

 - 对象的概念
 - 创建对象的字面量语法
 - 访问成员运算符
 - 创建对象的构造函数语法
    - this关键字
 - 添加和删除属性
    - delete关键字
 - 全局对象
    - Number / String / Boolean
    - Date / Math / RegEx / Array

#### BOM

 - window对象的属性和方法
 - history对象
    - forward() / back() / go()
 - location对象
 - navigator对象
 - screen对象

#### DOM

 - DOM树
 - 访问元素
    - getElementById() / querySelector()
    - getElementsByClassName() / getElementsByTagName() / querySelectorAll()
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
236
    - parentNode / previousSibling / nextSibling / children / firstChild / lastChild
237 238
- 操作元素
  - nodeValue
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
239
  - innerHTML / textContent / createElement() / createTextNode() / appendChild() / insertBefore() / removeChild()
240 241 242 243 244 245 246 247 248 249 250 251 252 253
  - className / id / hasAttribute() / getAttribute() / setAttribute() / removeAttribute()
- 事件处理
  - 事件类型
    - UI事件:load / unload / error / resize / scroll
    - 键盘事件:keydown / keyup / keypress
    - 鼠标事件:click / dbclick / mousedown / mouseup / mousemove / mouseover / mouseout
    - 焦点事件:focus / blur
    - 表单事件:input / change / submit / reset / cut / copy / paste / select
  - 事件绑定
    - HTML事件处理程序(不推荐使用,因为要做到标签与代码分离)
    - 传统的DOM事件处理程序(只能附加一个回调函数)
    - 事件监听器(旧的浏览器中不被支持)
  - 事件流:事件捕获 / 事件冒泡
  - 事件对象(低版本IE中的window.event)
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
254
    - target(有些浏览器使用srcElement)
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
    - type
    - cancelable
    - preventDefault()
    - stopPropagation()(低版本IE中的cancelBubble)
  - 鼠标事件 - 事件发生的位置
    - 屏幕位置:screenX和screenY
    - 页面位置:pageX和pageY
    - 客户端位置:clientX和clientY
  - 键盘事件 - 哪个键被按下了
    - keyCode属性
    - String.fromCharCode(event.keyCode)
  - HTML5事件
    - DOMContentLoaded
    - hashchange
    - beforeunload

#### JavaScript API

- HTML5中的API:geolocation / localStorage / sessionStorage / history

### 使用jQuery

#### jQuery概述

1. Write Less Do More(用更少的代码来完成更多的工作)
2. 使用CSS选择器来查找元素(更简单更方便)
3. 使用jQuery方法来操作元素(解决浏览器兼容性问题、应用于所有元素并施加多个方法)

#### 引入jQuery

- 下载jQuery的开发版和压缩版
- 从CDN加载jQuery

```HTML
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
    window.jQuery || 
        document.write('<script src="js/jquery-3.3.1.min.js"></script>')
</script>
```

#### 查找元素

- 选择器
  - \* / element / #id / .class / selector1, selector2
  - ancestor descendant / parent>child / previous+next / previous~siblings 
- 筛选器
  - 基本筛选器::not(selector) / :first / :last / :even / :odd / :eq(index) / :gt(index) / :lt(index) / :animated / :focus
  - 内容筛选器::contains('…') / :empty / :parent / :has(selector)
  - 可见性筛选器::hidden / :visible
  - 子节点筛选器::nth-child(expr) / :first-child / :last-child / :only-child
  - 属性筛选器:[attribute] / [attribute='value'] / [attribute!='value'] / [attribute^='value'] / [attribute$='value'] / [attribute|='value'] / [attribute~='value']
- 表单::input / :text / :password / :radio / :checkbox / :submit / :image / :reset / :button / :file / :selected / :enabled / :disabled / :checked

#### 执行操作

- 内容操作
  - 获取/修改内容:html() / text() / replaceWith() / remove()
  - 获取/设置元素:before() / after() / prepend() / append() / remove() / clone() / unwrap() / detach() / empty() / add()
  - 获取/修改属性:attr() / removeAttr() / addClass() / removeClass() / css()
  - 获取/设置表单值:val()
- 查找操作
  - 查找方法:find() /  parent() / children() / siblings() / next() / nextAll() / prev() / prevAll()
  - 筛选器:filter() / not() / has() / is() / contains()
  - 索引编号:eq()
- 尺寸和位置
  - 尺寸相关:height() / width() / innerHeight() / innerWidth() / outerWidth() / outerHeight()
  - 位置相关:offset() / position() / scrollLeft() / scrollTop()
- 特效和动画
  - 基本动画:show() / hide() / toggle()
  - 消失出现:fadeIn() / fadeOut() / fadeTo() / fadeToggle()
  - 滑动效果:slideDown() / slideUp() / slideToggle()
  - 自定义:delay() / stop() / animate()
- 事件
  - 文档加载:ready() / load()
  - 用户交互:on() / off()

#### 链式操作

#### 检测页面是否可用

```HTML
<script>
    $(document).ready(function() {
        
    });
</script>
```

```HTML
<script>
    $(function() {
        
    });
</script>
```

#### jQuery插件

- jQuery Validation
- jQuery Treeview
- jQuery Autocomplete
- jQuery UI

#### 避免和其他库的冲突

先引入其他库再引入jQuery的情况。

```HTML
<script src="other.js"></script>
<script src="jquery.js"></script>
<script>
	jQuery.noConflict();
    jQuery(function() {
        jQuery('div').hide();
    });
</script>
```

先引入jQuery再引入其他库的情况。

```HTML
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
377

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
<script src="jquery.js"></script>
<script src="other.js"></script>
<script>
    jQuery(function() {
        jQuery('div').hide();
    });
</script>
```

#### 使用Ajax

- 原生的Ajax
- 基于jQuery的Ajax
  - 加载内容
  - 提交表单

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
### 前端框架

#### 渐进式框架 - [Vue.js](<https://cn.vuejs.org/>)

##### 快速上手

1. 引入Vue的JavaScript文件,我们仍然推荐从CDN服务器加载它。

   ```HTML
   <script src="https://cdn.jsdelivr.net/npm/vue"></script>
   ```

2. 数据绑定(声明式渲染 )。

   ```HTML
   <div id="app">
   	<h1>{{ product }}库存信息</h1>
   </div>
   
   <script src="https://cdn.jsdelivr.net/npm/vue"></script>
   <script>
   	const app = new Vue({
   		el: '#app',
   		data: {
   			product: 'iPhone X'
   		}
   	});
   </script>
   ```

3. 条件与循环。

   ```HTML
   <div id="app">
   	<h1>库存信息</h1>
       <hr>
   	<ul>
   		<li v-for="product in products">
   			{{ product.name }} - {{ product.quantity }}
   			<span v-if="product.quantity === 0">
   				已经售罄
   			</span>
   		</li>
   	</ul>
   </div>
   
   <script src="https://cdn.jsdelivr.net/npm/vue"></script>
   <script>
   	const app = new Vue({
   		el: '#app',
   		data: {
   			products: [
   				{"id": 1, "name": "iPhone X", "quantity": 20},
   				{"id": 2, "name": "华为 Mate20", "quantity": 0},
   				{"id": 3, "name": "小米 Mix3", "quantity": 50}
   			]
   		}
   	});
   </script>
   ```

4. 计算属性。

   ```HTML
   <div id="app">
   	<h1>库存信息</h1>
   	<hr>
   	<ul>
   		<li v-for="product in products">
   			{{ product.name }} - {{ product.quantity }}
   			<span v-if="product.quantity === 0">
   				已经售罄
   			</span>
   		</li>
   	</ul>
   	<h2>库存总量:{{ totalQuantity }}台</h2>
   </div>
   
   <script src="https://cdn.jsdelivr.net/npm/vue"></script>
   <script>
   	const app = new Vue({
   		el: '#app',
   		data: {
   			products: [
   				{"id": 1, "name": "iPhone X", "quantity": 20},
   				{"id": 2, "name": "华为 Mate20", "quantity": 0},
   				{"id": 3, "name": "小米 Mix3", "quantity": 50}
   			]
   		},
   		computed: {
   			totalQuantity() {
   				return this.products.reduce((sum, product) => {
   					return sum + product.quantity
   				}, 0);
   			}
   		}
   	});
   </script>
   ```

5. 处理事件。

   ```HTML
   <div id="app">
   	<h1>库存信息</h1>
   	<hr>
   	<ul>
   		<li v-for="product in products">
   			{{ product.name }} - {{ product.quantity }}
   			<span v-if="product.quantity === 0">
   				已经售罄
   			</span>
   			<button @click="product.quantity += 1">
   				增加库存
   			</button>
   		</li>
   	</ul>
   	<h2>库存总量:{{ totalQuantity }}台</h2>
   </div>
   
   <script src="https://cdn.jsdelivr.net/npm/vue"></script>
   <script>
   	const app = new Vue({
   		el: '#app',
   		data: {
   			products: [
   				{"id": 1, "name": "iPhone X", "quantity": 20},
   				{"id": 2, "name": "华为 Mate20", "quantity": 0},
   				{"id": 3, "name": "小米 Mix3", "quantity": 50}
   			]
   		},
   		computed: {
   			totalQuantity() {
   				return this.products.reduce((sum, product) => {
   					return sum + product.quantity
   				}, 0);
   			}
   		}
   	});
   </script>
   ```

6. 用户输入。

   ```HTML
   <div id="app">
   	<h1>库存信息</h1>
   	<hr>
   	<ul>
   		<li v-for="product in products">
   			{{ product.name }} - 
   			<input type="number" v-model.number="product.quantity" min="0">
   			<span v-if="product.quantity === 0">
   				已经售罄
   			</span>
   			<button @click="product.quantity += 1">
   				增加库存
   			</button>
   		</li>
   	</ul>
   	<h2>库存总量:{{ totalQuantity }}台</h2>
   </div>
   
   <script src="https://cdn.jsdelivr.net/npm/vue"></script>
   <script>
   	const app = new Vue({
   		el: '#app',
   		data: {
   			products: [
   				{"id": 1, "name": "iPhone X", "quantity": 20},
   				{"id": 2, "name": "华为 Mate20", "quantity": 0},
   				{"id": 3, "name": "小米 Mix3", "quantity": 50}
   			]
   		},
   		computed: {
   			totalQuantity() {
   				return this.products.reduce((sum, product) => {
   					return sum + product.quantity
   				}, 0);
   			}
   		}
   	});
   </script>
   ```

7. 通过网络加载JSON数据。

   ```HTML
   <div id="app">
   	<h2>库存信息</h2>
   	<ul>
   		<li v-for="product in products">
   			{{ product.name }} - {{ product.quantity }}
   			<span v-if="product.quantity === 0">
   				已经售罄
   			</span>
   		</li>
   	</ul>
   </div>
   
   <script src="https://cdn.jsdelivr.net/npm/vue"></script>
   <script>
   	const app = new Vue({
   		el: '#app',
   		data: {
   			products: []
   		},
   		created() {
   			fetch('https://jackfrued.top/api/products')
   				.then(response => response.json())
   				.then(json => {
   					this.products = json
   				});
   		}
   	});
   </script>
   ```

##### 使用脚手架 - vue-cli

Vue为商业项目开发提供了非常便捷的脚手架工具vue-cli,通过工具可以省去手工配置开发环境、测试环境和运行环境的步骤,让开发者只需要关注要解决的问题。

1. 安装脚手架。
2. 创建项目。
3. 安装依赖包。
4. 运行项目。


#### UI框架 - [Element](<http://element-cn.eleme.io/#/zh-CN>)



#### 响应式布局框架 - [Bootstrap](<http://www.bootcss.com/>)
627 628 629 630 631 632 633 634 635 636 637 638 639

#### 特点

1. 支持主流的浏览器和移动设备
2. 容易上手
3. 响应式设计

#### 内容

1. 网格系统
2. 封装的CSS
3. 现成的组件
4. JavaScript插件