[关闭]
@fantaghiro 2014-12-17T09:15:02.000000Z 字数 50066 阅读 2480

妙味课堂-HTML5

学习笔记 js 前端 妙味课堂 HTML5


新增的语义化标签

新的页面结构以及宽松的语法规范

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <title></title>
  6. </head>
  7. <body>
  8. </body>
  9. </html>

新的结构化元素

语义化标签

  1. <hgroup>
  2. <h1>妙味课堂</h1>
  3. <h2>待您进入富有人情味的IT培训</h2>
  4. </hgroup>
  1. <nav><a href="#">链接</a><a href="#">链接</a></nav>
  2. ---
  3. <nav>
  4. <p><a href="#">妙味课堂</a></p>
  5. <p><a href="#">妙味课堂</a></p>
  6. </nav>
  7. ---
  8. <nav>
  9. <h2>妙味精品课程</h2>
  10. <ul>
  11. <li><a href="#">javascript</a></li>
  12. <li><a href="#">html+css</a></li>
  13. </ul>
  14. </nav>
  1. <figure>
  2. <img src="miaov.png" /> //注意没有alt
  3. <figcaption>妙味课堂 photo&copy 妙味趣学</figcaption>
  4. </figure>
  1. <p>
  2. 我们在每天早上<time>9:00</time>开始营业。
  3. </p>
  4. <p>
  5. 我在<time datetime="2008-02-14">情人节</time>有个约会。
  6. </p>
  1. <input type="text" list="valList" />
  2. <datalist id="valList">
  3. <option value="javascript">javascript</option>
  4. <option value="html">html</option>
  5. <option value="css">css</option>
  6. </datalist>
  1. <details>
  2. <summary>妙味课堂</summary>
  3. <p>国内将知名的IT培训机构</p>
  4. </details>
  1. <dialog>
  2. <dt>老师</dt>
  3. <dd>2+2等于?</dd>
  4. <dt>学生</dt>
  5. <dd>4</dd>
  6. <dt>老师</dt>
  7. <dd>答对了!</dd>
  8. </dialog>
  1. <form action="http://www.baidu.com" method="get">
  2. 用户:<input type="text" name="usr_name" />
  3. 公钥:<keygen name="security" />
  4. <input type="submit">
  5. </form>
  1. <progress max="100" value="76">
  2. <span>76</span>% //该行用于向下兼容
  3. </progress>

IE下的兼容

  1. <script>
  2. document.createElement('header');
  3. document.createElement('nav');
  4. document.createElement('footer');
  5. ...
  6. </script>

forms

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>表单的验证反馈</title>
  6. </head>
  7. <body>
  8. <form>
  9. <input type="text" required id="text" pattern="d{1,5}" maxlength="5">
  10. <input type="submit">
  11. </form>
  12. <script>
  13. var oText = document.getElementById('text');
  14. oText.addEventListener('invalid', fn, false);
  15. function fn(){
  16. // alert(this.validity);
  17. // alert(this.validity.valid);
  18. // alert(this.validity.valueMissing);
  19. // alert(this.validity.typeMismatch);
  20. // alert(this.validity.patternMismatch);
  21. // alert(this.validity.tooLong);
  22. ev.preventDefault();
  23. }
  24. //valueMissing: 当输入值为空时,返回true
  25. //typeMismatch: 当输入类型和要求的类型不一致时,返回true
  26. //patternMismatch: 当输入内容和预期的正则是不匹配时,返回true
  27. //tooLong: 当输入内容的长度超出了maxlength的限制,返回true
  28. </script>
  29. </body>
  30. </html>
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>表单的验证反馈</title>
  6. </head>
  7. <body>
  8. <form>
  9. <input type="text" id="text">
  10. <input type="submit">
  11. </form>
  12. <script>
  13. var oText = document.getElementById('text');
  14. oText.addEventListener('invalid', fn, false);
  15. oText.oninput = function(){
  16. if(this.value == '敏感词'){
  17. this.setCustomValidity('请不要输入敏感词');
  18. } else {
  19. this.setCustomValidity('');
  20. }
  21. }
  22. function fn(){
  23. alert(this.validity.customError);
  24. ev.preventDefault();
  25. }
  26. //customError: 不符合自定义验证的时候,返回true
  27. </script>
  28. </body>
  29. </html>
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form action="http://www.baidu.com">
  9. <input type="text" placeholder="请输入4—16个字符" name="user" pattern="d{4,16}" required />
  10. <input type="submit" value="提交" />
  11. <input type="submit" value="保存至草稿箱" formaction="http://www.qq.com" formnovalidate />
  12. </form>
  13. </body>
  14. </html>

HTML5新特性的浏览器支持情况

http://www.caniuse.com/#index

新的选择器

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>新的选择器</title>
  6. <script>
  7. // document.getElementById();
  8. // document.getElementsByTagName();
  9. //$('#div1') $('.box') $('ul li')
  10. window.onload = function(){
  11. //querySelector只能选择到一组元素中的第一个元素
  12. // var oDiv = document.querySelector('#div1'); //id形式
  13. // var oDiv = document.querySelector('div'); //标签形式
  14. // var oDiv = document.querySelector('.box'); //class形式
  15. var oDiv = document.querySelector('[title=hello]'); //通过属性值查找
  16. oDiv.style.background = 'red';
  17. //querySelectorAll 选择到一组元素
  18. var aDiv = document.querySelectorAll('.box');
  19. alert(aDiv.length);
  20. aDiv[1].style.background = "blue";
  21. var aBox = document.getElementsByClassName('box');
  22. alert(aBox.length);
  23. }
  24. </script>
  25. </head>
  26. <body>
  27. <div id="div1" class="box" title="hello">div</div>
  28. <div class="box">div2</div>
  29. <p class="box">p</p>
  30. </body>
  31. </html>

获取class列表属性

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>class列表属性</title>
  6. <script>
  7. window.onload = function(){
  8. var oDiv = document.querySelector('#div1');
  9. // alert(oDiv.classList); //box1 box2 box3
  10. // alert(typeof oDiv.classList); //object 类似于数组的对象
  11. // alert(oDiv.classList.length); //3
  12. oDiv.classList.add('box4');
  13. oDiv.classList.remove('box2');
  14. oDiv.classList.toggle('box3'); //如果原本有box3,就remove;如果没有,就add
  15. }
  16. </script>
  17. </head>
  18. <body>
  19. <div id="div1" class="box1 box2 box3">div</div>
  20. </body>
  21. </html>

JSON的新方法

parse()方法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>json的新方法</title>
  6. <script>
  7. /* 用eval
  8. var str = 'function show(){alert(123)}';
  9. eval(str); //eval能把字符串转为js
  10. show();
  11. */
  12. //eval可以解析任何字符串变成JS
  13. //parse只能解析json形式的字符串变成js (安全性更高一些)
  14. /* 报错,因为parse转不了function这样的字符串
  15. var str = 'function show(){alert(123)}';
  16. JSON.parse(str); //eval能把字符串转为js
  17. show();
  18. */
  19. /* 报错,这里的name没有加双引号就不行
  20. var str = '{name: "hello"}';
  21. var json = JSON.parse(str);
  22. alert(json.name);
  23. */
  24. var str = '{"name": "hello"}'; //一定是严格的json形式
  25. var json = JSON.parse(str);
  26. alert(json.name); //弹出hello
  27. </script>
  28. </head>
  29. <body>
  30. </body>
  31. </html>

stringify()方法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>json的新方法</title>
  6. <script>
  7. var json = {name: "hello"};
  8. var str = JSON.stringify(json);
  9. alert(str); //{"name":"hello"}
  10. </script>
  11. </head>
  12. <body>
  13. </body>
  14. </html>

对象拷贝

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>对象的拷贝</title>
  6. <script>
  7. var a = {
  8. name: 'hello'
  9. }
  10. /*
  11. //浅拷贝
  12. var b = {};
  13. for(var attr in a){
  14. b[attr] = a[attr];
  15. }
  16. */
  17. //用parse()和stringify()来实现
  18. var str = JSON.stringify(a);
  19. var b = JSON.parse(str);
  20. b.name = 'hi';
  21. alert(a.name); //hello
  22. </script>
  23. </head>
  24. <body>
  25. </body>
  26. </html>

data自定义数据

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>data自定义数据</title>
  6. <script>
  7. window.onload = function(){
  8. //getAttribute
  9. var oDiv = document.querySelector('#div1');
  10. alert(oDiv.dataset.miaov);
  11. alert(oDiv.dataset.miaovAll);
  12. }
  13. </script>
  14. </head>
  15. <body>
  16. <div id="div1" data-miaov="妙味" data-miaov-all="妙味课堂"></div>
  17. </body>
  18. </html>

延迟加载JS

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>JS加载</title>
  6. <script src="a.js"></script>
  7. <script src="b.js"></script>
  8. <script src="c.js"></script>
  9. <!-- 解决方法 -->
  10. <!-- 1. 把script放到body底部 -->
  11. <!-- 2. 给外部的script添加 defer = "defer" 这个是延迟到onload执行前触发 -->
  12. <!-- 3. 给script添加 async = "async" 加上异步,就代表是与其他内容并排加载的-->
  13. </head>
  14. <body>
  15. <!-- 默认上面所有的js都加载完毕之后,再加载img -->
  16. <img src="1.jpg" alt="">
  17. </body>
  18. </html>

历史管理

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>彩票</title>
  6. <script>
  7. window.onload = function(){
  8. var oInput = document.getElementById('input1');
  9. var oDiv = document.getElementById('div1');
  10. oInput.onclick = function(){
  11. var number = randomNum(35, 7);
  12. oDiv.innerHTML = number;
  13. }
  14. function randomNum(all, now){
  15. var arr = [];
  16. var newArr = [];
  17. for(var i=1; i<=all; i++){
  18. arr.push(i);
  19. }
  20. for(var i=0; i<now; i++){
  21. newArr.push(arr.splice(Math.floor(Math.random()*arr.length), 1));
  22. }
  23. return newArr;
  24. }
  25. }
  26. </script>
  27. </head>
  28. <body>
  29. <input type="button" value="35选7" id="input1">
  30. <div id="div1"></div>
  31. </body>
  32. </html>

采用onhashchange的方法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>彩票</title>
  6. <script>
  7. //onhashchange : 事件 当hash值有变化的时候,就会触发
  8. window.onload = function(){
  9. var oInput = document.getElementById('input1');
  10. var oDiv = document.getElementById('div1');
  11. var obj = {};
  12. oInput.onclick = function(){
  13. var number = randomNum(35, 7);
  14. oDiv.innerHTML = number;
  15. var oRD = Math.random();
  16. obj[oRD] = number;
  17. window.location.hash = oRD;
  18. }
  19. window.onhashchange = function(){
  20. var number = obj[window.location.hash.substring(1)] || '';
  21. oDiv.innerHTML = number;
  22. }
  23. function randomNum(all, now){
  24. var arr = [];
  25. var newArr = [];
  26. for(var i=1; i<=all; i++){
  27. arr.push(i);
  28. }
  29. for(var i=0; i<now; i++){
  30. newArr.push(arr.splice(Math.floor(Math.random()*arr.length), 1));
  31. }
  32. return newArr;
  33. }
  34. }
  35. </script>
  36. </head>
  37. <body>
  38. <input type="button" value="35选7" id="input1">
  39. <div id="div1"></div>
  40. </body>
  41. </html>

用html5的history对象来实现

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>彩票</title>
  6. <script>
  7. window.onload = function(){
  8. var oInput = document.getElementById('input1');
  9. var oDiv = document.getElementById('div1');
  10. var iNow = 1;
  11. oInput.onclick = function(){
  12. var number = randomNum(35, 7);
  13. oDiv.innerHTML = number;
  14. history.pushState(number, '', iNow++);
  15. }
  16. window.onpopstate = function(ev){
  17. var number = ev.state || '';
  18. oDiv.innerHTML = number;
  19. }
  20. function randomNum(all, now){
  21. var arr = [];
  22. var newArr = [];
  23. for(var i=1; i<=all; i++){
  24. arr.push(i);
  25. }
  26. for(var i=0; i<now; i++){
  27. newArr.push(arr.splice(Math.floor(Math.random()*arr.length), 1));
  28. }
  29. return newArr;
  30. }
  31. }
  32. </script>
  33. </head>
  34. <body>
  35. <input type="button" value="35选7" id="input1">
  36. <div id="div1"></div>
  37. </body>
  38. </html>

拖放事件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>拖放操作</title>
  6. <style>
  7. li {width: 100px; height: 30px; background: yellow; margin: 10px; list-style: none;}
  8. #div1 {width: 100px; height: 100px; background: red; margin: 200px;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var aLi = document.querySelectorAll('li');
  13. var oDiv = document.querySelector('#div1');
  14. var i = 0;
  15. for(var i=0; i<aLi.length; i++){
  16. aLi[i].ondragstart = function(){
  17. this.style.background = 'green';
  18. }
  19. aLi[i].ondrag = function(){
  20. // document.title = i++;
  21. }
  22. aLi[i].ondragend = function(){
  23. this.style.background = 'yellow';
  24. }
  25. }
  26. oDiv.ondragenter = function(){
  27. this.style.background = 'blue';
  28. }
  29. oDiv.ondragover = function(ev){
  30. // document.title = i++;
  31. // 要想触发drop事件,就必须在dragover当中阻止默认事件
  32. var ev = ev || window.event;
  33. ev.preventDefault();
  34. }
  35. oDiv.ondragleave = function(){
  36. this.style.background = 'red';
  37. }
  38. oDiv.ondrop = function(){
  39. alert(123);
  40. }
  41. }
  42. </script>
  43. </head>
  44. <body>
  45. <ul>
  46. <li draggable="true">a</li>
  47. <li draggable="true">b</li>
  48. <li draggable="true">c</li>
  49. </ul>
  50. <div id="div1"></div>
  51. </body>
  52. </html>

实现火狐下的拖拽

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>拖放操作——解决火狐的问题</title>
  6. <style>
  7. li {width: 100px; height: 30px; background: yellow; margin: 10px; list-style: none;}
  8. #div1 {width: 100px; height: 100px; background: red; margin: 200px;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var aLi = document.querySelectorAll('li');
  13. var oDiv = document.querySelector('#div1');
  14. var i = 0;
  15. for(var i=0; i<aLi.length; i++){
  16. aLi[i].ondragstart = function(ev){
  17. var ev = ev || window.event;
  18. ev.dataTransfer.setData('name', 'hello'); //火狐下面的操作 设置过这个就可以拖放了
  19. this.style.background = 'green';
  20. }
  21. aLi[i].ondrag = function(){
  22. // document.title = i++;
  23. }
  24. aLi[i].ondragend = function(){
  25. this.style.background = 'yellow';
  26. }
  27. }
  28. oDiv.ondragenter = function(){
  29. this.style.background = 'blue';
  30. }
  31. oDiv.ondragover = function(ev){
  32. // document.title = i++;
  33. // 要想触发drop事件,就必须在dragover当中阻止默认事件
  34. var ev = ev || window.event;
  35. ev.preventDefault();
  36. }
  37. oDiv.ondragleave = function(){
  38. this.style.background = 'red';
  39. }
  40. oDiv.ondrop = function(ev){
  41. var ev = ev || window.event;
  42. alert(ev.dataTransfer.getData('name')); //可以setData就可以getData
  43. alert(123);
  44. }
  45. }
  46. </script>
  47. </head>
  48. <body>
  49. <ul>
  50. <li draggable="true">a</li>
  51. <li draggable="true">b</li>
  52. <li draggable="true">c</li>
  53. </ul>
  54. <div id="div1"></div>
  55. </body>
  56. </html>

拖拽删除列表

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>拖拽删除列表</title>
  6. <style>
  7. li {width: 100px; height: 30px; background: yellow; margin: 10px; list-style: none;}
  8. #div1 {width: 100px; height: 100px; background: red; margin: 200px;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var aLi = document.querySelectorAll('li');
  13. var oDiv = document.querySelector('#div1');
  14. var oUl = document.querySelector('#ul1')
  15. var i = 0;
  16. for(var i=0; i<aLi.length; i++){
  17. aLi[i].index = i;
  18. aLi[i].ondragstart = function(ev){
  19. var ev = ev || window.event;
  20. ev.dataTransfer.setData('name', this.index);
  21. this.style.background = 'green';
  22. }
  23. aLi[i].ondrag = function(){
  24. // document.title = i++;
  25. }
  26. aLi[i].ondragend = function(){
  27. this.style.background = 'yellow';
  28. }
  29. }
  30. oDiv.ondragenter = function(){
  31. this.style.background = 'blue';
  32. }
  33. oDiv.ondragover = function(ev){
  34. // document.title = i++;
  35. // 要想触发drop事件,就必须在dragover当中阻止默认事件
  36. var ev = ev || window.event;
  37. ev.preventDefault();
  38. }
  39. oDiv.ondragleave = function(){
  40. this.style.background = 'red';
  41. }
  42. oDiv.ondrop = function(ev){
  43. var ev = ev || window.event;
  44. oUl.removeChild(aLi[ev.dataTransfer.getData('name')]);
  45. for(var i=0; i<aLi.length; i++){
  46. aLi[i].index = i;
  47. }
  48. }
  49. }
  50. </script>
  51. </head>
  52. <body>
  53. <ul id="ul1">
  54. <li draggable="true">a</li>
  55. <li draggable="true">b</li>
  56. <li draggable="true">c</li>
  57. </ul>
  58. <div id="div1"></div>
  59. </body>
  60. </html>

effectAllowed和setDragImage

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>拖放操作——effectAllowed和setDragImage</title>
  6. <style>
  7. li {width: 100px; height: 30px; background: yellow; margin: 10px; list-style: none;}
  8. #div1 {width: 100px; height: 100px; background: red; margin: 200px;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var aLi = document.querySelectorAll('li');
  13. var oDiv = document.querySelector('#div1');
  14. var oUl = document.querySelector('#ul1');
  15. var oImg = document.querySelector('img');
  16. var i = 0;
  17. for(var i=0; i<aLi.length; i++){
  18. aLi[i].ondragstart = function(ev){
  19. var ev = ev || window.event;
  20. ev.dataTransfer.setData('name', 'hello');
  21. ev.dataTransfer.effectAllowed = 'link';
  22. ev.dataTransfer.setDragImage(oImg, 0, 0);
  23. }
  24. aLi[i].ondrag = function(){
  25. }
  26. aLi[i].ondragend = function(){
  27. this.style.background = 'yellow';
  28. }
  29. }
  30. oDiv.ondragenter = function(){
  31. this.style.background = 'blue';
  32. }
  33. oDiv.ondragover = function(ev){
  34. var ev = ev || window.event;
  35. ev.preventDefault();
  36. }
  37. oDiv.ondragleave = function(){
  38. this.style.background = 'red';
  39. }
  40. oDiv.ondrop = function(ev){
  41. }
  42. }
  43. </script>
  44. </head>
  45. <body>
  46. <ul id="ul1">
  47. <li draggable="true">a</li>
  48. <li draggable="true">b</li>
  49. <li draggable="true">c</li>
  50. </ul>
  51. <div id="div1"></div>
  52. <img src="img/ppt.gif" style="display: none;">
  53. </body>
  54. </html>

拖放外部文件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>拖放操作——拖放外部文件</title>
  6. <style>
  7. #div1 {width: 200px; height: 200px; background: red; margin: 100px;}
  8. </style>
  9. <script>
  10. window.onload = function(){
  11. var oDiv = document.querySelector('#div1');
  12. var oImg = document.querySelector('img');
  13. var i = 0;
  14. oDiv.ondragenter = function(){
  15. this.innerHTML = '可以释放啦';
  16. }
  17. oDiv.ondragover = function(ev){
  18. var ev = ev || window.event;
  19. ev.preventDefault();
  20. }
  21. oDiv.ondragleave = function(){
  22. this.innerHTML = '将文件拖拽到此区域';
  23. }
  24. oDiv.ondrop = function(ev){
  25. var ev = ev || window.event;
  26. ev.preventDefault();
  27. var fs = ev.dataTransfer.files;
  28. // alert(fs); //object Filelist
  29. // alert(fs.length);
  30. // alert(fs[0].type); //文件类型
  31. var fd = new FileReader();
  32. fd.readAsDataURL(fs[0]);
  33. fd.onload = function(){ //上面文件信息读取成功之后就会走这个onload
  34. alert(this.result);
  35. }
  36. }
  37. }
  38. </script>
  39. </head>
  40. <body>
  41. <div id="div1">将文件拖拽到此区域</div>
  42. </body>
  43. </html>

图片预览功能

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>图片预览</title>
  6. <style>
  7. #div1 {width: 200px; height: 200px; background: red; margin: 100px;}
  8. </style>
  9. <script>
  10. window.onload = function(){
  11. var oDiv = document.querySelector('#div1');
  12. var oUl = document.getElementById('ul1')
  13. var oImg = document.querySelector('img');
  14. var i = 0;
  15. oDiv.ondragenter = function(){
  16. this.innerHTML = '可以释放啦';
  17. }
  18. oDiv.ondragover = function(ev){
  19. var ev = ev || window.event;
  20. ev.preventDefault();
  21. }
  22. oDiv.ondragleave = function(){
  23. this.innerHTML = '将文件拖拽到此区域';
  24. }
  25. oDiv.ondrop = function(ev){
  26. var ev = ev || window.event;
  27. ev.preventDefault();
  28. var fs = ev.dataTransfer.files;
  29. /*if(fs[0].type.indexOf('image') != -1){
  30. var fd = new FileReader();
  31. fd.readAsDataURL(fs[0]);
  32. fd.onload = function(){
  33. var oLi = document.createElement('li');
  34. var oImg = document.createElement('img');
  35. oImg.src = this.result;
  36. oLi.appendChild(oImg);
  37. oUl.appendChild(oLi);
  38. }
  39. } else {
  40. alert('亲,请上传图片类型!');
  41. } */
  42. for(var i=0; i<fs.length; i++){
  43. if(fs[i].type.indexOf('image') != -1){
  44. var fd = new FileReader();
  45. fd.readAsDataURL(fs[i]);
  46. fd.onload = function(){
  47. var oLi = document.createElement('li');
  48. var oImg = document.createElement('img');
  49. oImg.src = this.result;
  50. oLi.appendChild(oImg);
  51. oUl.appendChild(oLi);
  52. }
  53. } else {
  54. alert('亲,请上传图片类型!');
  55. }
  56. }
  57. }
  58. }
  59. </script>
  60. </head>
  61. <body>
  62. <div id="div1">将文件拖拽到此区域</div>
  63. <ul id="ul1"></ul>
  64. </body>
  65. </html>

拖拽购物车

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>拖拽购物车</title>
  6. <style>
  7. * {margin: 0; padding: 0;}
  8. li {list-style: none;}
  9. li {float: left; width: 200px; border: 1px solid #000; margin: 10px;}
  10. li img {width: 200px;}
  11. p {border-bottom: 1px dashed #333; height: 20px;}
  12. #div1 {width: 600px; border: 1px solid #000; height: 300px; clear: both;}
  13. .box1 {float: left; width: 200px;}
  14. .box2 {float: left; width: 200px;}
  15. .box3 {float: left; width: 200px;}
  16. #allMoney {float: right;}
  17. </style>
  18. <script>
  19. window.onload = function(){
  20. var aLi = document.getElementsByTagName('li');
  21. var oDiv = document.getElementById('div1');
  22. var obj = {};
  23. var iNum = 0;
  24. var allMoney = null;
  25. for(var i=0; i<aLi.length; i++){
  26. aLi[i].ondragstart = function(ev){
  27. var aP = this.getElementsByTagName('p');
  28. var ev = ev || window.event;
  29. ev.dataTransfer.setData('title', aP[0].innerHTML);
  30. ev.dataTransfer.setData('money', aP[1].innerHTML);
  31. ev.dataTransfer.setDragImage(this, 0, 0);
  32. }
  33. }
  34. oDiv.ondragover = function(ev){
  35. ev.preventDefault();
  36. }
  37. oDiv.ondrop = function(ev){
  38. ev.preventDefault();
  39. var sTitle = ev.dataTransfer.getData('title');
  40. var sMoney = ev.dataTransfer.getData('money');
  41. if(!obj[sTitle]){
  42. var oP = document.createElement('p');
  43. var oSpan = document.createElement('span');
  44. oSpan.className = 'box1';
  45. oSpan.innerHTML = 1;
  46. oP.appendChild(oSpan);
  47. var oSpan = document.createElement('span');
  48. oSpan.className = 'box2';
  49. oSpan.innerHTML = sTitle;
  50. oP.appendChild(oSpan);
  51. var oSpan = document.createElement('span');
  52. oSpan.className = 'box3';
  53. oSpan.innerHTML = sMoney;
  54. oP.appendChild(oSpan);
  55. oDiv.appendChild(oP);
  56. obj[sTitle] = 1;
  57. } else {
  58. var box1 = document.getElementsByClassName('box1');
  59. var box2 = document.getElementsByClassName('box2');
  60. for(var i=0; i<box2.length; i++){
  61. if(box2[i].innerHTML == sTitle){
  62. box1[i].innerHTML = parseInt(box1[i].innerHTML) + 1;
  63. }
  64. }
  65. }
  66. if(!allMoney){
  67. allMoney = document.createElement('div');
  68. allMoney.id = 'allMoney';
  69. }
  70. iNum += parseInt(sMoney.substring(1));
  71. allMoney.innerHTML = '¥' + iNum;
  72. oDiv.appendChild(allMoney);
  73. }
  74. }
  75. </script>
  76. </head>
  77. <body>
  78. <ul>
  79. <li draggable="true">
  80. <img src="images/1.png">
  81. <p>javascript语言精粹</p>
  82. <p>¥40</p>
  83. </li>
  84. <li draggable="true">
  85. <img src="images/2.png">
  86. <p>javascript权威指南</p>
  87. <p>¥120</p>
  88. </li>
  89. <li draggable="true">
  90. <img src="images/3.png">
  91. <p>精通javascript</p>
  92. <p>¥35</p>
  93. </li>
  94. <li draggable="true">
  95. <img src="images/4.png">
  96. <p>DOM编程艺术</p>
  97. <p>¥45</p>
  98. </li>
  99. </ul>
  100. <div id="div1">
  101. <!-- <p>
  102. <span class="box1">1</span>
  103. <span class="box2">DOM编程艺术</span>
  104. <span class="box3">¥45</span>
  105. </p>
  106. <p>
  107. <span class="box1">1</span>
  108. <span class="box2">DOM编程艺术</span>
  109. <span class="box3">¥45</span>
  110. </p>
  111. <div id="allMoney">¥90</div> -->
  112. </div>
  113. </body>
  114. </html>

跨文档消息通信

postMessage对象

通过iframe进行窗口间通信(同域)

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title></title>
  6. <script>
  7. /*
  8. 我们可以通过js去访问被包含页面的DOM元素
  9. */
  10. window.onload = function() {
  11. var oBtn = document.getElementById('btn');
  12. var oMyIframe = document.getElementById('myframe');
  13. oBtn.onclick = function() {
  14. //如果我们要操作一个iframe里面的dom元素,首先要获取到iframe引入的页面的window
  15. //oMyIframe.contentWindow -> 被iframe包含的页面的window对象
  16. //alert(oMyIframe.contentWindow);
  17. oMyIframe.contentWindow.document.body.style.background = 'red';
  18. }
  19. }
  20. </script>
  21. </head>
  22. <body>
  23. <input type="button" value="点击我,改变2.iframe.html的背景色" id="btn" />
  24. <iframe id="myframe" src="2.iframe.html"></iframe>
  25. </body>
  26. </html>

通过window.open来进行窗口间通信(同域)

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>无标题文档</title>
  6. <script>
  7. window.onload = function() {
  8. var oBtn = document.getElementById('btn');
  9. var oBtn2 = document.getElementById('btn2');
  10. var newWindow = null;
  11. oBtn.onclick = function() {
  12. //window.open 返回被打开窗口的window对象
  13. newWindow = window.open('4.window.open.html', '_blank');
  14. }
  15. oBtn2.onclick = function() {
  16. newWindow.document.body.style.background = 'red';
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <input type="button" value="点击我,开启一个新的窗口打开4.window.open.html页面" id="btn" />
  23. <input type="button" value="点击我,改变4.window.open.html页面的背景色" id="btn2" />
  24. </body>
  25. </html>

通过postMessage进行跨域的窗口通信 发送消息

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>无标题文档</title>
  6. <script>
  7. window.onload = function() {
  8. var oBtn = document.getElementById('btn');
  9. var oMyIframe = document.getElementById('myframe');
  10. /*
  11. postMessage : 可以通过这个方法给另外一个窗口发送信息
  12. 接收消息的窗口的window对象.postMessage();
  13. */
  14. oBtn.onclick = function() {
  15. //当本页面和包含页面不在同一个域名下的时候,这样操作就会有跨域操作安全限制问题。
  16. //oMyIframe.contentWindow.document.body.style.background = 'red';
  17. /*
  18. 第一个参数:发送的数据
  19. 第二个参数:接收数据的域名{带上协议}
  20. */
  21. oMyIframe.contentWindow.postMessage('1', 'http://www.b.com');
  22. //alert(oMyIframe.contentWindow.postMessage)
  23. }
  24. }
  25. </script>
  26. </head>
  27. <body>
  28. <input type="button" value="点击我,改变2.iframe.html的背景色" id="btn" />
  29. <iframe id="myframe" src="http://www.b.com/3.postMessage.html"></iframe>
  30. </body>
  31. </html>

通过message接收postMessage发来的信息

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script>
  7. window.onload = function(){
  8. /*
  9. message: 当窗口接收到通过postMessage发送过来的数据的时候触发
  10. */
  11. window.addEventListener('message', function(ev){
  12. //alert('b.com下的页面接受到了内容了')
  13. //message事件的事件对象保存了发送过来的内容
  14. //ev.data: 发送过来的数据
  15. //ev.origin: 发送消息的来源域
  16. //alert('我接受到了a.com页面发送过来的数据,内容是:'+ ev.data)
  17. //alert(ev.origin);
  18. if(ev.origin == 'www.a.com' && ev.data == '1'){
  19. document.body.style.background = 'red';
  20. }
  21. }, false);
  22. }
  23. </script>
  24. </head>
  25. <body>
  26. 这是b.com的potMessage.html页面
  27. </body>
  28. </html>

XMLHttpRequest Level 2

Ajax获取同域下的内容

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>无标题文档</title>
  6. <script>
  7. window.onload = function() {
  8. var oBtn = document.getElementById('btn');
  9. oBtn.onclick = function() {
  10. var xhr = new XMLHttpRequest();
  11. xhr.onreadystatechange = function() {
  12. if (xhr.readyState == 4) {
  13. if (xhr.status == 200) {
  14. alert(xhr.responseText);
  15. }
  16. }
  17. }
  18. xhr.open('get', 'ajax.php', true);
  19. xhr.send();
  20. }
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <input type="button" value="获取同域下内容" id="btn" />
  26. </body>
  27. </html>

IE下的跨域请求

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>无标题文档</title>
  6. <script>
  7. window.onload = function() {
  8. /*
  9. 在标准浏览器下,XMLHttpRequest对象已经是升级版本,支持了更多的特性,可以跨域了
  10. 但是,如果想实现跨域请求,还需要后端的相关配合才可以
  11. XMLHttpRequest : 增加很多功能,他也不推荐使用onreadystatechange这个事件来监听,推荐使用onload
  12. XDomainRequest : IE如果想实现跨域请求,则需要使用另外一个对象去实现
  13. */
  14. var oBtn = document.getElementById('btn');
  15. oBtn.onclick = function() {
  16. /*var xhr = new XMLHttpRequest();
  17. xhr.onreadystatechange = function() {
  18. if (xhr.readyState == 4) {
  19. if (xhr.status == 200) {
  20. alert(xhr.responseText);
  21. }
  22. }
  23. }
  24. xhr.open('get', 'http://www.b.com/ajax.php', true);
  25. xhr.send();*/
  26. var oXDomainRequest = new XDomainRequest();
  27. oXDomainRequest.onload = function() {
  28. alert(this.responseText);
  29. }
  30. oXDomainRequest.open('get', 'http://www.b.com/ajax.php', true);
  31. oXDomainRequest.send();
  32. }
  33. }
  34. </script>
  35. </head>
  36. <body>
  37. <input type="button" value="获取同域下内容" id="btn" />
  38. </body>
  39. </html>

Ajax实现无刷新上传

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>无标题文档</title>
  6. <style>
  7. #div1 {width: 300px; height: 30px; border: 1px solid #000; position: relative;}
  8. #div2 {width: 0; height: 30px; background: #CCC;}
  9. #div3 {width: 300px; height: 30px; line-height: 30px; text-align: center; position: absolute; left: 0; top: 0;}
  10. </style>
  11. <script>
  12. window.onload = function() {
  13. var oBtn = document.getElementById('btn');
  14. var oMyFile = document.getElementById('myFile');
  15. var oDiv1 = document.getElementById('div1');
  16. var oDiv2 = document.getElementById('div2');
  17. var oDiv3 = document.getElementById('div3');
  18. oBtn.onclick = function() {
  19. //alert(oMyFile.value); //获取到的是file控件的value值,这个内容是显示给你看的文字,不是我们选择的文件
  20. //oMyFile.files file控件中选择的文件列表对象
  21. //alert(oMyFile.files);
  22. //我们是要通过ajax把oMyFile.files[0]数据发送给后端
  23. /*for (var attr in oMyFile.files[0]) {
  24. console.log( attr + ' : ' + oMyFile.files[0][attr] );
  25. }*/
  26. var xhr = new XMLHttpRequest();
  27. xhr.onload = function() {
  28. //alert(1);
  29. //var d = JSON.parse(this.responseText);
  30. //alert(d.msg + ' : ' + d.url);
  31. alert('OK,上传完成');
  32. }
  33. //alert(xhr.upload);
  34. var oUpload = xhr.upload;
  35. //alert(oUpload);
  36. oUpload.onprogress = function(ev) {
  37. console.log(ev.total + ' : ' + ev.loaded);
  38. var iScale = ev.loaded / ev.total;
  39. oDiv2.style.width = 300 * iScale + 'px';
  40. oDiv3.innerHTML = iScale * 100 + '%';
  41. }
  42. xhr.open('post', 'post_file.php', true);
  43. xhr.setRequestHeader('X-Request-With', 'XMLHttpRequest');
  44. var oFormData = new FormData(); //通过FormData来构建提交数据
  45. oFormData.append('file', oMyFile.files[0]);
  46. xhr.send(oFormData);
  47. }
  48. }
  49. </script>
  50. </head>
  51. <body>
  52. <input type="file" id="myFile" /><input type="button" id="btn" value="上传" />
  53. <div id="div1">
  54. <div id="div2"></div>
  55. <div id="div3">0%</div>
  56. </div>
  57. </body>
  58. </html>

form的上传方式

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>无标题文档</title>
  6. </head>
  7. <body>
  8. <form method="post" action="post_file.php" enctype="multipart/form-data">
  9. <input type="file" name="file" />
  10. <input type="submit" value="上传" />
  11. </form>
  12. </body>
  13. </html>

websocket

  1. var http = require('http');
  2. var serv = http.createServer(function(req, res) {
  3. console.log('有人进来了');
  4. /*res.writeHeader(200, {
  5. 'content-type' : 'text/html;charset="utf-8"'
  6. });*/
  7. /*res.writeHeader(404, {
  8. 'content-type' : 'text/html;charset="utf-8"'
  9. });
  10. res.write('你要访问的页面资源不存在!');
  11. res.end();*/
  12. console.log(req);
  13. }).listen(8888);
  14. console.log('服务器开启成功');
  1. var http = require('http');
  2. var fs = require('fs');
  3. var documentRoot = 'E:/HTML5/websocket/www';
  4. var httpServer = http.createServer(function(req, res) {
  5. var url = req.url;
  6. //console.log(url);
  7. var file = documentRoot + url;
  8. console.log(file);
  9. fs.readFile(file, function(err, data) {
  10. if (err) {
  11. res.writeHeader(404, {
  12. 'content-type' : 'text/html;charset="utf-8"'
  13. });
  14. res.write('<h1>404</h1><p>你要找的页面被LEO吃了</p>');
  15. res.end();
  16. } else {
  17. res.writeHeader(200, {
  18. 'content-type' : 'text/html;charset="utf-8"'
  19. });
  20. res.write(data);
  21. res.end();
  22. }
  23. });
  24. }).listen(8888);
  1. var http = require('http');
  2. var fs = require('fs');
  3. var io = require('socket.io');
  4. var documentRoot = 'E:/HTML5/websocket/www';
  5. var httpServer = http.createServer(function(req, res) {
  6. var url = req.url;
  7. //console.log(url);
  8. var file = documentRoot + url;
  9. console.log(file);
  10. fs.readFile(file, function(err, data) {
  11. if (err) {
  12. res.writeHeader(404, {
  13. 'content-type' : 'text/html;charset="utf-8"'
  14. });
  15. res.write('<h1>404</h1><p>你要找的页面被LEO吃了</p>');
  16. res.end();
  17. } else {
  18. res.writeHeader(200, {
  19. 'content-type' : 'text/html;charset="utf-8"'
  20. });
  21. res.write(data);
  22. res.end();
  23. }
  24. });
  25. }).listen(8888);
  26. var socket = io.listen(httpServer);
  27. socket.sockets.on('connection', function(socket) {
  28. //socket
  29. //console.log('有人通过socket连接进来了');
  30. socket.emit('hello', '欢迎');
  31. /*socket.on('hellotoo', function(data) {
  32. console.log(data);
  33. })*/
  34. //socket.broadcast.emit('a');
  35. socket.broadcast.emit('a', '有新人进来了');
  36. socket.on('move', function(data) {
  37. socket.broadcast.emit('move2', data);
  38. });
  39. });

HTML5的离线存储(applicationCache)

搭建离线应用程序

manifest文件

  1. CACHE MANIFEST
  2. 2.png
  3. 1.jpg
  4. FALLBACK
  5. style1.css style2.css
  6. NETWORK
  7. a.jpg

Web Workers

Web Worker简单应用

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Web Worker</title>
  6. <script type="text/javascript">
  7. var w1 = new Worker('test.js');
  8. w1.postMessage('hi');
  9. w1.onmessage = function(ev){
  10. alert(ev.data);
  11. }
  12. </script>
  13. </head>
  14. <body>
  15. </body>
  16. </html>
  1. //test.js
  2. self.onmessage = function(ev){ //这里的self就是指自己本身,也就是w1这个web worker
  3. //alert(ev.data); //web worker中的js里面有些命令是不认的,例如alert就不认。
  4. self.postMessage(ev.data + '妙味课堂');
  5. }

使用Worker提高像素显字的代码性能

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>像素显字</title>
  6. <style>
  7. body {background: #000; color: white; font-size: 30px;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var oC = document.getElementById('c1');
  13. var oGC = oC.getContext('2d');
  14. var aLi = document.getElementsByTagName('li');
  15. for(var i=0; i<aLi.length; i++){
  16. aLi[i].onclick = function(){
  17. var str = this.innerHTML;
  18. var h = 180;
  19. oGC.clearRect(0, 0, oC.width, oC.height);
  20. oGC.font = h + 'px impact';
  21. oGC.fillStyle = 'red';
  22. oGC.textBaseline = 'top';
  23. var w = oGC.measureText(str).width;
  24. oGC.fillText(str, (oC.width - w) / 2, (oC.height - h) / 2);
  25. var oImg = oGC.getImageData((oC.width - w) / 2, (oC.height - h) / 2, w, h);
  26. oGC.clearRect(0, 0, oC.width, oC.height);
  27. //下面启用的Worker
  28. var w1 = new Worker('canvas.js');
  29. w1.postMessage(w*h);
  30. w1.onmessage = function(ev){
  31. var arr = ev.data;
  32. var newImg = oGC.createImageData(w, h);
  33. for(var i = 0; i<arr.length; i++){
  34. newImg.data[arr[i]*4] = oImg.data[4*arr[i]];
  35. newImg.data[arr[i]*4+1] = oImg.data[4*arr[i]+1];
  36. newImg.data[arr[i]*4+2] = oImg.data[4*arr[i]+2];
  37. newImg.data[arr[i]*4+3] = oImg.data[4*arr[i]+3];
  38. }
  39. oGC.putImageData(newImg, (oC.width - w) / 2, (oC.height - h) / 2);
  40. }
  41. }
  42. }
  43. }
  44. </script>
  45. </head>
  46. <body>
  47. <canvas id="c1" width="400" height="400"></canvas>
  48. <ul style="float: left">
  49. <li></li>
  50. <li></li>
  51. <li></li>
  52. <li></li>
  53. </ul>
  54. </body>
  55. </html>
  1. //canvas.js
  2. function randomArr(iAll, iNow){ //从iAll个数里面随机取出iNow个数
  3. var arr = [];
  4. var newArr = [];
  5. for(var i=0; i<iAll; i++){
  6. arr.push(i);
  7. }
  8. for(var i=0; i<iNow; i++){
  9. newArr.push(arr.splice(Math.floor(Math.random()*arr.length), 1));
  10. }
  11. return newArr;
  12. }
  13. self.onmessage = function(ev){
  14. var arr = randomArr(ev.data, ev.data/10);
  15. self.postMessage(arr);
  16. }

HTML5其他功能

内容编辑

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <div contenteditable="true" style="height: 200px; width: 200px; background: red;">aaaaaaaaaaa</div>
  9. </body>
  10. </html>

语言输入

桌面提醒

地理信息(Geolocation)

单次定位请求

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script>
  7. // LBS:基于地理信息的应用
  8. window.onload = function(){
  9. var oInput = document.getElementById('input1');
  10. var oT = document.getElementById('t1');
  11. oInput.onclick = function(){
  12. navigator.geolocation.getCurrentPosition(function(position){
  13. oT.value += '经度:' + position.coords.longitude+'\n';
  14. oT.value += '纬度 :' + position.coords.latitude+'\n';
  15. oT.value += '准确度 :' + position.coords.accuracy+'\n';
  16. oT.value += '海拔 :' + position.coords.altitude+'\n';
  17. oT.value += '海拔准确度 :' + position.coords.altitudeAcuracy+'\n';
  18. oT.value += '行进方向 :' + position.coords.heading+'\n';
  19. oT.value += '地面速度 :' + position.coords.speed+'\n';
  20. oT.value += '时间戳:' + new Date(position.timestamp)+'\n';
  21. }, function(err){
  22. //err.code // 失败所对应的编号
  23. alert( err.code );
  24. }, {
  25. enableHighAcuracy : true,
  26. timeout : 5000,
  27. maximumAge : 5000
  28. });
  29. };
  30. }
  31. </script>
  32. </head>
  33. <body>
  34. <input type="button" value="请求" id="input1"><br>
  35. <textarea id="t1" style="width: 400px; height: 400px; border: 1px solid #000"></textarea>
  36. </body>
  37. </html>

多次定位请求

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script>
  7. // LBS:基于地理信息的应用
  8. window.onload = function(){
  9. var oInput = document.getElementById('input1');
  10. var oT = document.getElementById('t1');
  11. var timer = null;
  12. oInput.onclick = function(){
  13. timer = navigator.geolocation.watchPosition(function(position){
  14. oT.value += '经度:' + position.coords.longitude+'\n';
  15. oT.value += '纬度 :' + position.coords.latitude+'\n';
  16. oT.value += '准确度 :' + position.coords.accuracy+'\n';
  17. oT.value += '海拔 :' + position.coords.altitude+'\n';
  18. oT.value += '海拔准确度 :' + position.coords.altitudeAcuracy+'\n';
  19. oT.value += '行进方向 :' + position.coords.heading+'\n';
  20. oT.value += '地面速度 :' + position.coords.speed+'\n';
  21. oT.value += '时间戳:' + new Date(position.timestamp)+'\n';
  22. }, function(err){
  23. //err.code // 失败所对应的编号
  24. alert( err.code );
  25. navigator.geolocation.clearWatch(timer);
  26. }, {
  27. enableHighAcuracy : true,
  28. timeout : 5000,
  29. maximumAge : 5000,
  30. frequency : 1000
  31. });
  32. };
  33. }
  34. </script>
  35. </head>
  36. <body>
  37. <input type="button" value="请求" id="input1"><br>
  38. <textarea id="t1" style="width: 400px; height: 400px; border: 1px solid #000"></textarea>
  39. </body>
  40. </html>

地图应用

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>地图应用</title>
  6. <style>
  7. #div1{ width:400px; height:400px; border:1px #000 solid;}
  8. </style>
  9. <script src="h ttp://api.map.baidu.com/api?v=1.3"></script>
  10. <script>
  11. window.onload = function(){
  12. var oInput = document.getElementById('input1');
  13. oInput.onclick = function(){
  14. navigator.geolocation.getCurrentPosition(function(position){
  15. var y = position.coords.longitude;
  16. var x = position.coords.latitude;
  17. var map = new BMap.Map("div1");
  18. var pt = new BMap.Point(y, x);
  19. map.centerAndZoom(pt, 14);
  20. map.enableScrollWheelZoom();
  21. var myIcon = new BMap.Icon("miaov.png", new BMap.Size(30,30));
  22. var marker2 = new BMap.Marker(pt,{icon:myIcon}); // 创建标注
  23. map.addOverlay(marker2);
  24. var opts = {
  25. width : 200, // 信息窗口宽度
  26. height: 60, // 信息窗口高度
  27. title : "妙味课堂" // 信息窗口标题
  28. }
  29. var infoWindow = new BMap.InfoWindow("IT培训机构", opts); // 创建信息窗口对象
  30. map.openInfoWindow(infoWindow,pt); //开启信息窗口
  31. });
  32. };
  33. };
  34. </script>
  35. </head>
  36. <body>
  37. <input type="button" value="请求" id="input1" />
  38. <div id="div1"></div>
  39. </body>
  40. </html>

本地存储

Storage API

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>本地存储</title>
  6. <style></style>
  7. <script>
  8. window.onload = function(){
  9. var aInput = document.getElementsByTagName('input');
  10. aInput[0].onclick = function(){ //设置
  11. // sessionStorage : 临时性存储
  12. // localStorage : 永久性存储
  13. //window.sessionStorage.setItem('name', aInput[3].value);
  14. window.localStorage.setItem('name', aInput[3].value);
  15. }
  16. aInput[1].onclick = function(){ //获取
  17. //alert(window.sessionStorage.getItem('name'));
  18. alert(window.localStorage.getItem('name'));
  19. }
  20. aInput[2].onclick = function(){ //删除
  21. //window.localStorage.removeItem('name');
  22. window.localStorage.clear(); //删除全部数据
  23. }
  24. }
  25. </script>
  26. </head>
  27. <body>
  28. <input type="button" value="设置">
  29. <input type="button" value="获取">
  30. <input type="button" value="删除">
  31. <input type="text">
  32. </body>
  33. </html>

保存注册信息

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>保存注册信息</title>
  6. <style></style>
  7. <script>
  8. window.onload = function(){
  9. var aInput = document.getElementsByTagName('input');
  10. var oT = document.getElementById('t1');
  11. if(window.localStorage.getItem('name')){
  12. aInput[0].value = window.localStorage.getItem('name');
  13. }
  14. if(window.localStorage.getItem('sex')){
  15. for(var i=1; i<aInput.length; i++){
  16. if(aInput[i].value == window.localStorage.getItem('sex')){
  17. aInput[i].checked = true;
  18. }
  19. }
  20. }
  21. if(window.localStorage.getItem('ta')){
  22. oT.value = window.localStorage.getItem('ta');
  23. }
  24. window.onunload = function(){
  25. if(aInput[0].value){
  26. window.localStorage.setItem('name', aInput[0].value);
  27. }
  28. for(var i=1; i<aInput.length; i++){
  29. if(aInput[i].checked == true){
  30. window.localStorage.setItem('sex', aInput[i]);
  31. }
  32. }
  33. if(oT.value){
  34. window.localStorage.setItem('ta', oT.value);
  35. }
  36. }
  37. }
  38. </script>
  39. </head>
  40. <body>
  41. <p>
  42. 用户名:<input type="text">
  43. </p>
  44. <p>
  45. 性别:男 <input type="radio" name="sex"><input type="radio" name="sex">
  46. </p>
  47. 内容:<textarea id="t1"></textarea>
  48. </body>
  49. </html>

storage事件

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>本地存储</title>
  6. <style></style>
  7. <script>
  8. window.onload = function(){
  9. var aInput = document.getElementsByTagName('input');
  10. aInput[0].onclick = function(){
  11. window.localStorage.setItem('name', aInput[3].value);
  12. }
  13. aInput[1].onclick = function(){
  14. alert(window.localStorage.getItem('name'));
  15. }
  16. aInput[2].onclick = function(){
  17. window.localStorage.removeItem('name');
  18. }
  19. window.addEventListener('storage', function(ev){ //当前页面的事件不会触发,其他页面会触发
  20. alert(123);
  21. console.log(ev.key);
  22. console.log(ev.newValue);
  23. console.log(ev.oldValue);
  24. console.log(ev.storageArea);
  25. console.log(ev.url);
  26. }, false);
  27. }
  28. </script>
  29. </head>
  30. <body>
  31. <input type="button" value="设置">
  32. <input type="button" value="获取">
  33. <input type="button" value="删除">
  34. <input type="text">
  35. </body>
  36. </html>

同步购物车

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>同步购物车</title>
  6. <style></style>
  7. <script>
  8. window.onload = function(){
  9. var aInput = document.getElementsByTagName('input');
  10. for(var i=0; i<aInput.length; i++){
  11. aInput[i].onclick = function(){
  12. if(this.checked){
  13. window.localStorage.setItem('sel', this.value);
  14. } else {
  15. window.localStorage.setItem('unSel', this.value)
  16. }
  17. }
  18. }
  19. window.addEventListener('storage', function(ev){
  20. if(ev.key == 'sel'){
  21. for(var i=0; i<aInput.length; i++){
  22. if(ev.newValue == aInput[i].value){
  23. aInput[i].checked = true;
  24. }
  25. }
  26. } else if(ev.key == 'unSel'){
  27. for(var i=0; i<aInput.length; i++){
  28. if(ev.newValue == aInput[i].value){
  29. aInput[i].checked = false;
  30. }
  31. }
  32. }
  33. }, false);
  34. }
  35. </script>
  36. </head>
  37. <body>
  38. <input type="checkbox" value="香蕉">香蕉<br>
  39. <input type="checkbox" value="苹果">苹果<br>
  40. <input type="checkbox" value="西瓜">西瓜<br>
  41. <input type="checkbox" value="哈密瓜">哈密瓜<br>
  42. </body>
  43. </html>

行走日记

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>无标题文档</title>
  6. <style>
  7. *{ margin:0; padding:0;}
  8. li{ list-style:none;}
  9. #div1,#div2{ float:left; width:400px; height:400px; border:1px #000000 solid; margin:10px; position:relative; overflow:hidden;}
  10. #div2 ul{ position:absolute; left:0; width:2000px;}
  11. #div2 ul .box{ width:400px; height:400px; float:left; overflow:hidden;}
  12. #div2 #childUl{ width:400px;}
  13. #childUl li{ width:400px; border-bottom:1px #666666 dashed;}
  14. #map{ width:100%; height:380px;}
  15. </style>
  16. <script src="move.js"></script>
  17. <script src="http://api.map.baidu.com/api?v=1.3"></script>
  18. <script>
  19. window.onload = function(){
  20. var oDiv = document.getElementById('div1');
  21. var aInput = oDiv.getElementsByTagName('input');
  22. var oT = document.getElementById('t1');
  23. var iNow = window.localStorage.getItem('num') || 0;
  24. var oChildUl = document.getElementById('childUl');
  25. var aChildLi = oChildUl.getElementsByTagName('li');
  26. var oUl = document.getElementById('ul1');
  27. var aBox = oUl.getElementsByClassName('box');
  28. var aBox1_input = aBox[1].getElementsByTagName('input');
  29. var aBox1_div = aBox[1].getElementsByTagName('div');
  30. var oBox2_input = aBox[2].getElementsByTagName('input')[0];
  31. var index = 0;
  32. if( window.localStorage.getItem('num') ){
  33. for(var i=0;i<iNow;i++){
  34. var oLi = document.createElement('li');
  35. oLi.innerHTML = window.localStorage.getItem('title'+i);
  36. oChildUl.appendChild( oLi );
  37. }
  38. changeLi();
  39. }
  40. aInput[1].onclick = function(){ //保存本地
  41. window.localStorage.setItem('title'+iNow,aInput[0].value);
  42. window.localStorage.setItem('ta'+iNow,oT.value);
  43. createLi();
  44. iNow++;
  45. window.localStorage.setItem('num',iNow);
  46. };
  47. aInput[2].onclick = function(){ //提交后台
  48. };
  49. aInput[3].onclick = function(){ //删除所有的数据
  50. window.localStorage.clear();
  51. };
  52. aInput[4].onclick = function(){
  53. var This = this;
  54. navigator.geolocation.getCurrentPosition(function(position){
  55. var y = position.coords.longitude;
  56. var x = position.coords.latitude;
  57. if( This.checked ){
  58. window.localStorage.setItem('y'+iNow,y);
  59. window.localStorage.setItem('x'+iNow,x);
  60. }
  61. else{
  62. window.localStorage.removeItem('y'+iNow);
  63. window.localStorage.removeItem('x'+iNow);
  64. }
  65. });
  66. };
  67. function createLi(){
  68. var oLi = document.createElement('li');
  69. oLi.innerHTML = window.localStorage.getItem('title'+iNow);
  70. oChildUl.appendChild( oLi );
  71. changeLi();
  72. }
  73. function changeLi(){
  74. for(var i=0;i<aChildLi.length;i++){
  75. aChildLi[i].index = i;
  76. aChildLi[i].onclick = function(){
  77. startMove(oUl,{left : -aBox[0].offsetWidth });
  78. aBox1_div[0].innerHTML = window.localStorage.getItem('title'+this.index);
  79. aBox1_div[1].innerHTML = window.localStorage.getItem('ta'+this.index);
  80. if( window.localStorage.getItem('y'+this.index) ){
  81. aBox1_input[1].disabled = false;
  82. }
  83. else{
  84. aBox1_input[1].disabled = true;
  85. }
  86. index = this.index;
  87. };
  88. }
  89. }
  90. aBox1_input[0].onclick = function(){
  91. startMove(oUl,{left : 0});
  92. };
  93. aBox1_input[1].onclick = function(){
  94. startMove(oUl,{left : -2*aBox[0].offsetWidth});
  95. var map = new BMap.Map("map");
  96. var y = window.localStorage.getItem('y'+index);
  97. var x = window.localStorage.getItem('x'+index);
  98. var title = window.localStorage.getItem('title'+index);
  99. var text = window.localStorage.getItem('ta'+index);
  100. var point = new BMap.Point(y, x);
  101. map.centerAndZoom(point,15);
  102. map.enableScrollWheelZoom();
  103. var marker1 = new BMap.Marker(point);
  104. map.addOverlay(marker1);
  105. var opts = {
  106. width : 250, // 信息窗口宽度
  107. height: 100, // 信息窗口高度
  108. title : title // 信息窗口标题
  109. }
  110. var infoWindow = new BMap.InfoWindow(text, opts);
  111. map.openInfoWindow(infoWindow,point);
  112. };
  113. oBox2_input.onclick = function(){
  114. startMove(oUl,{left : -aBox[0].offsetWidth });
  115. };
  116. };
  117. </script>
  118. </head>
  119. <body>
  120. <div id="div1">
  121. 标题:<input type="text" /><br />
  122. 内容:<textarea id="t1" style="height:300px; width:300px;"></textarea><br />
  123. <input type="button" value="保存本地" />
  124. <input type="button" value="同步服务器" />
  125. <input type="button" value="删除所有数据" />
  126. <input type="checkbox" />记录地图位置
  127. </div>
  128. <div id="div2">
  129. <ul id="ul1">
  130. <li class="box">
  131. <ul id="childUl">
  132. </ul>
  133. </li>
  134. <li class="box">
  135. <input type="button" value="后退" />
  136. <input style="float:right" type="button" value="前进" />
  137. <div></div>
  138. <div></div>
  139. </li>
  140. <li class="box">
  141. <input type="button" value="后退" />
  142. <div id="map"></div>
  143. </li>
  144. </ul>
  145. </div>
  146. </body>
  147. </html>

音频和视频

音视频的基本操作

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>HTML5音视频</title>
  6. <style></style>
  7. <script>
  8. window.onload = function(){
  9. var oA = document.getElementById('a1');
  10. var oV = document.getElementById('v1');
  11. var oInput = document.getElementById('input1');
  12. var aS = document.getElementsByTagName('source');
  13. // setInterval(function(){
  14. // console.log(oA.currentTime);
  15. // }, 1000);
  16. oA.currentTime = 60; //音频从60秒开始播放 currentTime属性既可以读取也可以设置
  17. console.log(oA.duration);
  18. console.log(oA.volume);
  19. console.log(oA.muted); //静音就返回真;否则就返回假
  20. console.log(oA.paused);
  21. console.log(oA.ended);
  22. console.log(oA.error);
  23. console.log(oA.currentSrc);
  24. oV.onmouseover = function(){
  25. this.play();
  26. }
  27. oV.onmouseout = function(){
  28. this.pause();
  29. }
  30. oInput.onclick = function(){
  31. aS[0].src = 'XXXX.mp4';
  32. aS[1].src = 'XXXX.ogv';
  33. oV.load();
  34. }
  35. oV.poster = 'images/1.png';
  36. oV.addEventListener('ended', function(){
  37. alert(123);
  38. }, false)
  39. }
  40. </script>
  41. </head>
  42. <body>
  43. <!-- <audio controls src="johann_sebastian_bach_air.mp3"></audio>
  44. <audio controls src="johann_sebastian_bach_air.ogg"></audio>
  45. <video controls src="Intermission-Walk-in_512kb.mp4"></video>
  46. <video controls src="Intermission-Walk-in.ogv"></video> -->
  47. <!-- <video controls>
  48. //先找第一个source,如果浏览器不支持第一个,就找下一个
  49. <source src="Intermission-Walk-in.ogv"></source>
  50. <source src="Intermission-Walk-in_512kb.mp4"></source>
  51. </video> -->
  52. <audio id="a1" controls autobuffer src="johann_sebastian_bach_air.mp3"></audio>
  53. <input type="button" value="重新加载" id="input1">
  54. <video id="v1">
  55. //先找第一个source,如果浏览器不支持第一个,就找下一个
  56. <source src="Intermission-Walk-in.ogv"></source>
  57. <source src="Intermission-Walk-in_512kb.mp4"></source>
  58. </video>
  59. </body>
  60. </html>

带声音的导航

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>带声音的导航</title>
  6. <style>
  7. * {margin: 0; padding: 0;}
  8. #ul1 {width: 1000px; height: 30px;}
  9. #ul1 li {list-style: none; width: 100px; height: 30px; background: red; color: white; border: 1px solid #000; float: left; text-align: center; line-height: 30px;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var aLi = document.getElementsByTagName('li');
  14. var oA = document.getElementById('a1');
  15. for(var i=0; i<aLi.length; i++){
  16. aLi[i].onmouseover = function(){
  17. //this.getAttribute('au')
  18. oA.src = 'http://s8.qhimg.com/share/audio/piano1/' + this.getAttribute('au') + '4.mp3';
  19. oA.play();
  20. }
  21. }
  22. }
  23. </script>
  24. </head>
  25. <body>
  26. <ul id="ul1">
  27. <li au="c">00001</li>
  28. <li au="d">00002</li>
  29. <li au="e">00003</li>
  30. <li au="f">00004</li>
  31. <li au="g">00005</li>
  32. <li au="a">00006</li>
  33. <li au="b">00007</li>
  34. </ul>
  35. <audio id="a1"></audio>
  36. </body>
  37. </html>

视频与canvas结合

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>视频与canvas结合</title>
  6. <style></style>
  7. <script>
  8. window.onload = function(){
  9. var oV = document.getElementById('v1');
  10. var oC = document.getElementById('c1');
  11. var oGC = oC.getContext('2d');
  12. oC.width = oV.videoWidth;
  13. oC.height = oV.videoHeight;
  14. setInterval(function(){
  15. oGC.drawImage(oV, 0, 0); //drawImage里面也可以添加视频
  16. }, 30);
  17. }
  18. </script>
  19. </head>
  20. <body>
  21. <video controls id="v1">
  22. <source src="Intermission-Walk-in.ogv"></source>
  23. <source src="Intermission-Walk-in.mp4"></source>
  24. </video>
  25. <canvas id="c1"></canvas>
  26. </body>
  27. </html>

自制播放器

  1. <!DOCTYPE html>
  2. <head>
  3. <title>无标题文档</title>
  4. <style>
  5. *{ margin:0; padding:0;}
  6. #div1{ width:300px; height:30px; background:#666; overflow:hidden; position:relative;}
  7. #div2{ width:60px; height:30px; background:red; position:absolute; left:0; top:0;}
  8. #div3{ width:300px; height:10px; background:#666; overflow:hidden; position:relative; top:10px;}
  9. #div4{ width:60px; height:10px; background:yellow; position:absolute; left:240px; top:0;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oV = document.getElementById('v1');
  14. var aInput = document.getElementsByTagName('input');
  15. var oDiv1 = document.getElementById('div1');
  16. var oDiv2 = document.getElementById('div2');
  17. var oDiv3 = document.getElementById('div3');
  18. var oDiv4 = document.getElementById('div4');
  19. var disX = 0;
  20. var disX2 = 0;
  21. var timer = null;
  22. aInput[0].onclick = function(){
  23. if( oV.paused ){
  24. oV.play();
  25. this.value = '暂停';
  26. nowTime();
  27. timer = setInterval(nowTime,1000);
  28. }else{
  29. oV.pause();
  30. this.value = '播放';
  31. clearInterval(timer);
  32. }
  33. };
  34. aInput[2].value = changeTime(oV.duration);
  35. aInput[3].onclick = function(){
  36. if( oV.muted ){
  37. oV.volume = 1;
  38. this.value = '静音';
  39. oV.muted = false;
  40. }else{
  41. oV.volume = 0;
  42. this.value = '取消静音';
  43. oV.muted = true;
  44. }
  45. };
  46. aInput[4].onclick = function(){ //全屏方法
  47. if (oV.requestFullscreen) {
  48. oV.requestFullscreen();
  49. } else if (oV.msRequestFullscreen) {
  50. oV.msRequestFullscreen();
  51. } else if (oV.mozRequestFullScreen) {
  52. oV.mozRequestFullScreen();
  53. } else if (oV.webkitRequestFullscreen) {
  54. oV.webkitRequestFullscreen();
  55. }
  56. };
  57. function nowTime(){
  58. aInput[1].value = changeTime(oV.currentTime);
  59. var scale = oV.currentTime/oV.duration;
  60. oDiv2.style.left = scale * 240 + 'px';
  61. }
  62. function changeTime(iNum){
  63. iNum = parseInt( iNum );
  64. var iH = toZero(Math.floor(iNum/3600));
  65. var iM = toZero(Math.floor(iNum%3600/60));
  66. var iS = toZero(Math.floor(iNum%60));
  67. return iH + ':' +iM + ':' + iS;
  68. }
  69. function toZero(num){
  70. if(num<=9){
  71. return '0' + num;
  72. }
  73. else{
  74. return '' + num;
  75. }
  76. }
  77. oDiv2.onmousedown = function(ev){
  78. var ev = ev || window.event;
  79. disX = ev.clientX - oDiv2.offsetLeft;
  80. document.onmousemove = function(ev){
  81. var ev = ev || window.event;
  82. var L = ev.clientX - disX;
  83. if(L<0){
  84. L = 0;
  85. }else if(L>oDiv1.offsetWidth - oDiv2.offsetWidth){
  86. L = oDiv1.offsetWidth - oDiv2.offsetWidth;
  87. }
  88. oDiv2.style.left = L + 'px';
  89. var scale = L/(oDiv1.offsetWidth - oDiv2.offsetWidth);
  90. oV.currentTime = scale * oV.duration;
  91. nowTime();
  92. };
  93. document.onmouseup = function(){
  94. document.onmousemove = null;
  95. };
  96. return false;
  97. };
  98. oDiv4.onmousedown = function(ev){
  99. var ev = ev || window.event;
  100. disX2 = ev.clientX - oDiv4.offsetLeft;
  101. document.onmousemove = function(ev){
  102. var ev = ev || window.event;
  103. var L = ev.clientX - disX2;
  104. if(L<0){
  105. L = 0;
  106. }else if(L>oDiv3.offsetWidth - oDiv4.offsetWidth){
  107. L = oDiv3.offsetWidth - oDiv4.offsetWidth;
  108. }
  109. oDiv4.style.left = L + 'px';
  110. var scale = L/(oDiv3.offsetWidth - oDiv4.offsetWidth);
  111. oV.volume = scale;
  112. };
  113. document.onmouseup = function(){
  114. document.onmousemove = null;
  115. };
  116. return false;
  117. };
  118. };
  119. </script>
  120. </head>
  121. <body>
  122. <video id="v1">
  123. <source src="Intermission-Walk-in.ogv"></source>
  124. <source src="Intermission-Walk-in_512kb.mp4"></source>
  125. </video><br />
  126. <input type="button" value="播放" />
  127. <input type="button" value="00:00:00" />
  128. <input type="button" value="00:00:00" />
  129. <input type="button" value="静音" />
  130. <input type="button" value="全屏" />
  131. <div id="div1">
  132. <div id="div2"></div>
  133. </div>
  134. <div id="div3">
  135. <div id="div4"></div>
  136. </div>
  137. </body>
  138. </html>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注