广义的HTML5是指包括HTML,CSS3,Javascript 在内的一套的技术组合。

    主要作用:
    减少网页浏览器对于需要插件的丰富性网络应用服务。
    提供更多能有效加强网络应用得标准集。

    《HTML5 新增标签》
    header, nav, section, aside, article, footer,这些标签只是起到语义化的作用,本身视觉效果和DIV并没有大的区别。

    HTML5新增表单输入框的类型:
    type=”email”
    type=”url”
    type=”date”
    type=”number”
    type=”range”
    type=”search”
    type=”color”




    input标签并不需要结束标签,应该写在form表单里面,且应当有name属性。

    placeholder属性
    增加未输入值前的提示信息。

    autocomplete属性:
    autocomplete 属性规定输入字段是否应该启用自动完成功能。
    自动完成允许浏览器预测对字段的输入。当用户在字段开始键入时,浏览器基于之前键入过的值,应该显示出在字段中填写的选项。
    注释:autocomplete 属性适用于

    ,以及下面的 类型:text, search, url, telephone, email, password, datepickers, range 以及 color。
    默认值为on,用户可以设置为off。

    例子:
    URL 的autocomplete=”off”。

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <form name="form1" action="/">
    11. <input name="email" type="email" placeholder="请输入邮箱">
    12. <input name="url" type="url" placeholder="请输入网址" autocomplete="off">
    13. <input name="date" type="date">
    14. <input name="number" type="number" placeholder="请输入数字" autofocus="true">
    15. <input name="search" type="search" placeholder="请输入...">
    16. <input name="range" type="range">
    17. <input name="color" type="color">
    18. <input name="reset" type="reset" value="reset">
    19. <input name="submit" type="submit" value="submit">
    20. </form>
    21. </body>
    22. </html>

    《HTML 制图 和 Canvas》:
    Canvas初识示例:
    注意: 2d需要小写,c.getContext 是浏览器兼容性判断。canvas中的文字将会在浏览器不支持的情况下显示。

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Canvas</title>
    8. </head>
    9. <body>
    10. <div style="border: 1px solid green; width: 300px; height: 300px">
    11. <canvas id="canvas1" width="300px" height="300px">
    12. 您的浏览器不支canvas,请升级您的浏览器!
    13. </canvas>
    14. </div>
    15. <script>
    16. window.onload = function () {
    17. var c = document.getElementById('canvas1');
    18. if (c.getContext) {
    19. var ctx = c.getContext("2d");
    20. ctx.font = "20px Arial";
    21. ctx.fillText("Hello World!", 100, 100);
    22. }
    23. };
    24. </script>
    25. </body>
    26. </html>

    《HTML5 多媒体 & Video,Audio》:
    video元素支持多个source元素,source元素在浏览器使用的是第一个识别(支持)的视频文件。

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. <style>
    9. html,body{
    10. margin: 0;
    11. }
    12. </style>
    13. </head>
    14. <body>
    15. <video width="50%" height="50%" controls="controls" autoplay="autoplay">
    16. <source src="banner.mp4" type="video/mp4">
    17. 您的浏览器不支video,请升级您的浏览器!
    18. </video>
    19. </body>
    20. </html>

    《HTML5的 Web存储功能》:
    会话存储(window.sessionStorage):生命周期为关闭浏览器窗口,同一个窗口下数据可共享,刷新页面依然可以访问的到。
    本地存储(window.localStorage):永久生效,除非手动删除,可多窗口共享。
    注: 可以在F12,在Application Tab中查看。

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Storage</title>
    8. </head>
    9. <body>
    10. <button onclick="setSessionStorage()">setSessionStorage</button>
    11. <button onclick="setLocalStorage()">setLocalStorage</button>
    12. <script>
    13. function setSessionStorage() {
    14. window.sessionStorage.setItem("session11","Hello: Session");
    15. }
    16. function setLocalStorage() {
    17. window.localStorage.setItem("local11","Hello: World");
    18. }
    19. </script>
    20. </body>
    21. </html>

    《HTML5 拖拽》

    image.png
    代码示例:

    注: ondragover 会默认屏蔽调 ondrop,所以要调用事件的preventDefault()。

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Drag</title>
    8. <style>
    9. .big-box {
    10. width: 200px;
    11. height: 200px;
    12. border: 1px solid black;
    13. float: left;
    14. }
    15. .small-box {
    16. width: 80px;
    17. height: 80px;
    18. border: 1px solid green;
    19. float: left;
    20. }
    21. </style>
    22. </head>
    23. <body>
    24. <div id="big" ondragenter="dragenter(event)" ondragover="dragover(event)" ondragleave="dragleave(event)" ondrop="drop(event)" class="big-box">
    25. </div>
    26. <div id="small" ondragstart="dragstart(event)" ondrag="drag(event)" ondragend="dragend(event)" class="small-box" draggable="true">
    27. </div>
    28. <script>
    29. function dragstart(e){
    30. console.log('start');
    31. e.dataTransfer.setData('source', e.target.id);
    32. }
    33. function drag(e){
    34. console.log('draging');
    35. }
    36. function dragend(e){
    37. console.log('end');
    38. }
    39. function dragenter(e){
    40. console.log('Big: enter');
    41. }
    42. function dragover(e){
    43. console.log('Big: over');
    44. e.preventDefault();
    45. }
    46. function dragleave(e){
    47. console.log('Big: leave');
    48. }
    49. function drop(e){
    50. console.log('Big: drop');
    51. var source = document.getElementById(e.dataTransfer.getData("source"));
    52. e.target.appendChild(source);
    53. }
    54. </script>
    55. </body>
    56. </html>

    【题目1】 !DOCTYPE是什么?有什么作用?不写它可能会出现什么问题?
    DOCTYPE是document type的简写,它并不是 HTML 标签,也没有结束标签,它是一种标记语言的文档类型声明,即告诉浏览器当前 HTML 是用什么版本编写的。DOCTYPE的声明必须是 HTML 文档的第一行,位于html标签之前。大多数Web文档的顶部都有doctype声明。很少人会去注意 doctype ,但在遵循标准的任何Web文档中,它都是一项必需的元素。doctype会影响代码验证,并决定了浏览器最终如何显示你的 Web文档。

    作用: **DOCTYPE声明中指出阅读程序应该用什么规则来解释文档中的标记。在Web文档的情况下,阅读程序通常是浏览器或者校验器这样的一个程序,规则是W3C所发布的一个文档类型定义 DTD 中包含的规则。制作一个符合标准的网页,DOCTYPE声明是是不可缺少的,它在Web设计中用来说明你用的XHTML或者HTML是什么版本,如果不做DOCTYPE声明或声明不正确的情况下,将有可能导致你的标识与CSS失效,从而令你网页的布局变乱,造成网页在浏览器中不能正常的显示。我们还可以通过W3C提供的验证工具来检查页面的内容是否符合在DOCTYPE中声明的标准。

    【题目2】如何处理html5在IE低版本(IE8及以下)的兼容问题,请例举两种。
    (1)可以在元素中添加不兼容提示信息;

    1. <convas width="300" height="300">
    2. 您的浏览器不支持convas标签,请升级浏览器!
    3. <convas>

    (2)可以在JS中判断API是否存在;

    1. <body>
    2. <canvas id="myCanvas" width="300" height="300">
    3. 您的浏览器不支持convas标签,请升级浏览器!
    4. <canvas>
    5. </body>
    6. <script>
    7. window.onload() = function () {
    8. var api = document.getElementById("myCanvas")
    9. if (!api.getContext) {
    10. window.alert('您的浏览器不支持canvas,请升级浏览器!')
    11. }
    12. }
    13. </script>


    【题目3】表单元素,如input,关于设置name,以下说法正确的是: (B)**

    • A.可以不设置,意义不大
    • B.需要设置,表单提交的时候通过name提取元素内容
    • C.需要设置,如果某表单里有元素没有设置name,该表单无法提交

    **【题目4】请使用canvas画一个五角星。
    要求:画布宽高为400px;填充色为#E4EF00;边框宽度为3px;颜色为red,效果如图:
    《HTML5 简介 & HTML5 新特性》 - 图2
    第一版本:并不是真正的五角星,因为旋转角度不精确。
    注:strokeStyle外边框样式,fillStyle为填充样式。

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Canvas</title>
    8. </head>
    9. <body>
    10. <div style="border: 1px solid green; width: 400px; height: 400px">
    11. <canvas id="canvas1" width="400px" height="400px">
    12. 您的浏览器不支canvas,请升级您的浏览器!
    13. </canvas>
    14. </div>
    15. <script>
    16. window.onload = function () {
    17. var c = document.getElementById('canvas1');
    18. if (c.getContext) {
    19. var ctx = c.getContext("2d");
    20. ctx.fillStyle = "#E4EF00";
    21. ctx.lineWidth = 3;
    22. ctx.strokeStyle = "red";
    23. ctx.beginPath();
    24. ctx.moveTo(200, 0);
    25. ctx.lineTo(150, 150);
    26. ctx.lineTo(0, 150);
    27. ctx.lineTo(130, 230);
    28. ctx.lineTo(100, 400);
    29. ctx.lineTo(200, 280);
    30. ctx.lineTo(300, 400);
    31. ctx.lineTo(270, 230);
    32. ctx.lineTo(400, 150);
    33. ctx.lineTo(250, 150);
    34. ctx.lineTo(200, 0);
    35. ctx.fill();
    36. ctx.stroke();
    37. }
    38. };
    39. </script>
    40. </body>
    41. </html>

    第二版本:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Canvas</title>
    8. </head>
    9. <body>
    10. <div style="border: 1px solid green; width: 400px; height: 400px">
    11. <canvas id="canvas1" width="400px" height="400px">
    12. 您的浏览器不支canvas,请升级您的浏览器!
    13. </canvas>
    14. </div>
    15. <script>
    16. function drawStar(cxt , r , R , x , y , rot , borderWidth , borderStyle , fillStyle){
    17. cxt.beginPath();
    18. for( var i = 0 ; i < 5 ; i ++){
    19. cxt.lineTo(Math.cos((18+72*i - rot)/180*Math.PI) * R + x ,- Math.sin((18+72*i - rot )/180*Math.PI) * R + y);
    20. cxt.lineTo(Math.cos((54+72*i - rot)/180*Math.PI) * r + x ,- Math.sin((54+72*i - rot )/180*Math.PI) * r + y);
    21. }
    22. cxt.closePath();
    23. cxt.lineWidth = borderWidth;
    24. cxt.strokeStyle = borderStyle;
    25. cxt.fillStyle = fillStyle;
    26. cxt.fill();
    27. cxt.stroke();
    28. }
    29. var c = document.getElementById('canvas1');
    30. if (c.getContext) {
    31. var cxt = c.getContext("2d");
    32. drawStar(cxt,80,200,200,200,0,3,'red','#E4EF00');
    33. }
    34. </script>
    35. </body>
    36. </html>

    【题目5】下面关于cookie和H5web存储的说法中,正确的是(多选)

    • A. cookie数据存储在服务器,H5web数据存储在本地
    • B. H5web存储可存储的数据量大于cookie允许存储的数据量
    • C. localStorage 对象存储的数据没有时间限制
    • D.关闭浏览器会话之后,cookie数据将会·消失

    : B, C

    【题目6】以下代码中(关于H5拖拽),ev.preventDefault()的作用是?
    :阻止默认事件行为。

    《HTML5 简介 & HTML5 新特性》 - 图3