html5學習筆記(-),html5新特性介紹教程

一、html5語義化

二、HTML5新特性

 1、HTML5新特性的瀏覽器支持情況

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

 2、新的選擇器

 querySelector 獲取單個元素 如: 

  1. var obj=document.querySelector('.box');
  2. var obj1=document.querySelector('#box');
  3. var obj2=document.querySelector('[name="username"]);

 querySelectorAll 獲取多個元素集合 如:

  1. var obj=document.querySelectorAll('.box');

 getElementsByClassName 獲取class的元素集合

3、獲取class列表屬性 : 獲取對象的className的集合

classList   

  • length :  class的長度
  • add()  :  添加class方法
  • remove()  :  刪除class方法
  • toggle() :  切換class方法 

 如:

  1. <div class="a b"></div> alert(document.querySelector('#div').classList) //輸出a b

 4、data自定義數據

 -dataset :是data-的集合

 data-name :  dataset.name
 data-name-first  :  dataset.nameFirst 
如:                 

  1. <div id="box" data-name="zhangsan" data-user-sex="nan"></div>
  2. var obj=document.querySelector('#box');
  3. console.log(obj.dataset.name)
  4. console.log(obj.dataset.userSex)

 -Data數據在jquery mobile中有着重要作用

5、JSON的新方法

  • -parse() : 把字符串轉成json, 類似eval()方法

字符串中的屬性要嚴格的加上引號

  • -stringify() : 把json轉化成字符串

           會自動的把雙引號加上

  •  -新方法與eval的區別:eval都能轉換,是JSON.parse()只能轉換json
  •  -新方法的應用

          深度克隆新對象 如:                

  1. //以往的方法
  2. var a={"name":"zhangsan"};
  3. var b=a;
  4. b.name="lisi";
  5. alert(a.name); //lisi
  6.  
  7. //html5 -json新方法
  8. var a={"name":"zhangsan"};
  9. var str=JSON.stringify(a); //轉換成字符串
  10. var b=JSON.parse(str); //轉換成對象
  11. b.name="lisi";
  12. alert(a.name); //zhangsan
  • -如何其他瀏覽器做到兼容

                http://www.json.org/去下載json2.js

 6、延遲加載JS 

  •  JS的加載會影響後面的內容加載(js是單線程)

 很多瀏覽器都採用了並行加載JS,但還是會影響其他內容

  •  Html5的defer和async

           defer : 延遲加載,會按順序執行,在onload執行前被觸發(或者簡單理解爲是在頁面加載完時執行,會按順序)
           async : 異步加載,加載完就觸發,有順序問題(並行加載,而且不會按照順序加載)
           如: 

  1. <script src="a.js" defer></script>
  •  Labjs庫:(第三方插件:最大化提高性能,即能異步,也能延遲)

如:labjs.com/documentation.php

labjs中wait()等待前面的加載完才執行

  1. <script>
  2. $LSB
  3. .script('a.js')
  4. .script('b.js')
  5. .script('c.js').wait()
  6. .script('d.js').wait();
  7. </script>                  

 seaJs,requireJs框架集成了延遲和異步加載

7、歷史管理

onhashchange  

        :改變hash值來管理    如:改變hash方法   window.location.hash=”cont”;

         監聽hash根據hash改變現實內容: html5事件  onhashchange 案例:                  

  1. <ul class="nav">
  2. <li data-hash="index">首頁</li>
  3. <li data-hash="news">新聞</li>
  4. <li data-hash="sports">體育</li>
  5. </ul>
  6. <div class="cont">
  7. <div data-content="index">首頁內容</div>
  8. <div data-content="news">新聞內容</div>
  9. <div data-content="sports">體育內容</div>
  10. </div>
  11. <script>
  12. //知識點 querySelector querySelectorAll選擇器
  13. //substring截取字符串
  14. //onhashchange html5的監聽事件
  15. window.onload=function(){
  16. var oLi=document.querySelectorAll('li');
  17. var oCont=document.querySelector('.cont').querySelectorAll('div');
  18. for (var i = 0; i < oLi.length; i++) {
  19. oLi[i].onclick=function(){
  20. window.location.hash=this.dataset.hash;
  21. }
  22. };
  23. function changeCont(){//根據hash改變內容
  24. var hash=window.location.hash.substring(1);
  25. for(var i=0;i<oCont.length;i++){
  26. if(hash==oCont[i].dataset.content){
  27. oCont[i].style.display="block";
  28. }else{
  29. oCont[i].style.display="none";
  30. }
  31. }
  32. }
  33. changeCont();
  34. window.onhashchange=function(){
  35. changeCont();
  36. }
  37. }

 history  :

  • 服務器下運行
  • pushState :  三個參數 :數據  標題(都沒實現)  地址(可選)
  • popstate事件 :  讀取數據   event.state

注意:網址是虛假的,需在服務器指定對應頁面,不然刷新找不到頁面

          案例:
                 

  1. <ul class="nav">
  2. <li data-hash="index">首頁</li>
  3. <li data-hash="news">新聞</li>
  4. <li data-hash="sports">體育</li>
  5. </ul>
  6. <div class="cont">
  7. <div data-content="index">首頁內容</div>
  8. <div data-content="news">新聞內容</div>
  9. <div data-content="sports">體育內容</div>
  10. </div>
  11. <script>
  12. window.οnlοad=function(){
  13. var oLi=document.querySelectorAll('li');
  14. var oCont=document.querySelector('.cont').querySelectorAll('div');
  15. var iNow=0;
  16. for (var i = 0; i < oLi.length; i++) {
  17. oLi[i].οnclick=function(){
  18. var hash=this.dataset.hash;
  19. history.pushState(hash,'',iNow++);//第二個參數是標題很多瀏覽器沒實現便可不寫,加上第三個參數便可以改變網址,但是是虛假的網址刷新就沒有了,所以得配合後端使用
  20. changeCont(hash);
  21. }
  22. };
  23. function changeCont(hash){//根據hash改變內容
  24. for(var i=0;i<oCont.length;i++){
  25. if(hash==oCont[i].dataset.content){
  26. oCont[i].style.display="block";
  27. }else{
  28. oCont[i].style.display="none";
  29. }
  30. }
  31. }
  32. window.onpopstate=function(ev){
  33. console.log(1);
  34. changeCont(event.state);
  35. }
  36. }
  37. </script>

8、拖放事件 

draggable :

                設置爲true,元素就可以拖拽了 (如單獨設置draggable爲true,前臺拖拽的時候只是有拖拽的拖影,而不是整個元素都能拖動)

拖拽元素事件 :  

事件對象爲被拖拽元素(在給元素綁定拖拽事件時,必須得給元素設置draggable=”true”屬性)

  • dragstart ,  拖拽前觸發 
  • drag ,拖拽前、拖拽結束之間,連續觸發
  • dragend , 拖拽結束觸發

案例:           

  1. <style>
  2. #box{height:100px;width:100px;border:1px solid #999;}
  3. </style>
  4. <div id="box" draggable="true"></div>
  5. <script>
  6. window.onload=function(){
  7. var oDrag =document.querySelector('#box');
  8. oDrag.ondragstart=function(){
  9. console.log("拖拽前");
  10. }
  11. oDrag.ondrag=function(){
  12. console.log("拖拽進行中");
  13. }
  14. oDrag.ondragend=function(){
  15. console.log("結束");
  16. }
  17. }
  18. </script>

目標元素事件 :  

事件對象爲目標元素(目標元素通常是被拖拽元素,碰撞到最終的元素,如拖拽上傳,上傳框就是目標元素)

  • dragenter ,  進入目標元素觸發,相當於mouseover (元素進入不會觸發,一定是鼠標進入才觸發)
  • dragover  ,進入目標、離開目標之間,連續觸發
  • dragleave ,  離開目標元素觸發,相當於mouseout
  • drop  ,  在目標元素上釋放鼠標觸發 (需要在dragover事件中設置阻止默認事件,不然拖動的時候鼠標移至都是禁止的狀態,就無法釋放,也就不能觸發drop事件)

案例:

  1. <style>
  2. #box,#mubiao{height:100px;width:100px;border:1px solid #999;}
  3. </style>
  4. <div id="box" draggable="true"></div>
  5. <div id="mubiao"></div>
  6. <script>
  7. window.onload=function(){
  8. var index=0;
  9. var oMubiao =document.querySelector('#mubiao');
  10. oMubiao.ondragenter=function(){
  11. this.style.background="#000";
  12. }
  13. oMubiao.ondragleave=function(){
  14. this.style.background="#999";
  15. }
  16. oMubiao.ondragover=function(ev){
  17. ev.preventDefault();
  18. console.log(index++);
  19. }
  20. oMubiao.ondrop=function(){
  21. alert("鼠標放開了");
  22. }
  23. }
  24. </script>

事件的執行順序 :drop不觸發的時候
                dragstart  >  drag >  dragenter >  dragover >  dragleave > dragend 
事件的執行順序 :drop觸發的時候(dragover的時候阻止默認事件)
                dragstart  >  drag >  dragenter >  dragover >  drop > dragend
不能釋放的光標和能釋放的光標不一樣,需要觸發drop釋放事件,在上例中

解決火狐下的問題

(火狐不設置的話只支持dragstart事件其他都不支持)

          必須設置dataTransfer對象纔可以拖拽除圖片外的其他標籤
          案例:如在dragstart的event對象中設置dataTransfer.setData 設置一個參數
                
dataTransfer對象(屬於event事件對象)

  • setData() : 設置數據 key和value(必須是字符串)
  • getData() : 獲取數據,根據key值,獲取對應的value
  • effectAllowed 

          effectAllowed : 設置光標樣式(none, copy, copyLink, copyMove, link, linkMove, move, all 和 uninitialized)

  • etDragImage  (設置鼠標的位置)

           三個參數:指定的元素,座標X,座標Y

  • files 

          獲取外部拖拽的文件,返回一個filesList列表
          filesList下有個type屬性,返回文件的類型

關於:setDate,getDate,effectAllowed,setDragImage的案例 

  1. <style>
  2. #box,#mubiao{height:100px;width:100px;border:1px solid #999;}
  3. </style>
  4. <div id="box" draggable="true"></div>
  5. <div id="mubiao"></div>
  6. <img id="img" src="http://www.baidu.com/img/bd_logo1.png" alt="">
  7. <script>
  8. window.onload=function(){
  9. var index=0;
  10. var oDrag =document.querySelector('#box');
  11. var oMubiao =document.querySelector('#mubiao');
  12. oDrag.ondragstart=function(ev){
  13. var oImg=document.querySelector('#img');
  14. console.log("拖拽前");
  15. ev.dataTransfer.setData('name','demo');
  16. ev.dataTransfer.effectAllowed="link";
  17. ev.dataTransfer.setDragImage(this,50,50);
  18. //此處this可以是其他對象,也可以試圖片對象比如換
  19. //ev.dataTransfer.setDragImage(oImg,50,50);
  20. // 擴展拖拽排序,百度音樂播放器,拖拽排列歌曲列表
  21. }
  22. oDrag.ondrag=function(){
  23. console.log("拖拽進行中");
  24. }
  25. oDrag.ondragend=function(){
  26. console.log("結束");
  27. }
  28. oMubiao.ondragenter=function(){
  29. this.style.background="#000";
  30. }
  31. oMubiao.ondragleave=function(){
  32. this.style.background="#999";
  33. }
  34. oMubiao.ondragover=function(ev){
  35. ev.preventDefault();
  36. console.log(index++);
  37. }
  38. oMubiao.ondrop=function(ev){
  39. alert(ev.dataTransfer.getData('name'));
  40. }
  41. }
  42. </script>              

 關於:files的案例              

  1. <style>
  2. #mubiao{height:200px;width:200px;border:1px solid #999;}
  3. </style>
  4. <div id="mubiao"></div>
  5. <script>
  6. window.onload=function(){
  7. var oMubiao =document.querySelector('#mubiao');
  8. oMubiao.ondragenter=function(){
  9. this.innerHTML = '可以釋放啦';
  10. }
  11. oMubiao.ondragleave=function(ev){
  12. this.innerHTML = '請在此區域釋放鼠標';
  13. }
  14. oMubiao.ondragover=function(ev){
  15. ev.preventDefault();
  16. }
  17. oMubiao.ondrop=function(ev){
  18. ev.preventDefault();
  19. console.log(ev.dataTransfer.files[0].type)
  20. }
  21. }
  22. </script>

9、FileReader(讀取文件信息)

readAsDataURL

 參數爲要讀取的文件對象,將文件讀取爲DataUrl 

onload

當讀取文件成功完成的時候觸發此事件
this. result , 來獲取讀取的文件數據,如果是圖片,將返回base64格式的圖片數據

實例
                拖拽刪除列表
                拖拽購物車
                上傳圖片預覽功能
 簡單案例:
            

  1. <style>
  2. #mubiao{height:200px;width:200px;border:1px solid #999;}
  3. </style>
  4. <div id="mubiao"></div>
  5. <img src="" id="img" alt="">
  6. <script>
  7. window.onload=function(){
  8. var oImg=document.querySelector('#img');
  9. var oMubiao =document.querySelector('#mubiao');
  10. oMubiao.ondragenter=function(){
  11. this.innerHTML = '可以釋放啦';
  12. }
  13. oMubiao.ondragleave=function(ev){
  14. this.innerHTML = '請在此區域釋放鼠標';
  15. }
  16. oMubiao.ondragover=function(ev){
  17. ev.preventDefault();
  18. }
  19. oMubiao.ondrop=function(ev){
  20. ev.preventDefault();
  21. var fs=ev.dataTransfer.files; //獲取文件對象
  22. console.dir(fs[0]);
  23. var fr=new FileReader(); //實例化讀取文件信息對象
  24. fr.readAsDataURL(fs[0]); //將文件讀取爲dataUrl格式
  25. fr.onload=function(){ //讀取文件成功後事件
  26. //alert(this.result);
  27. oImg.src=this.result; //假如上傳的是圖片可以複製給一個圖片對象
  28. }
  29.  
  30. }
  31. }
  32. </script>

10、可做練習:

  • 理解新的選擇器
  • 獲取class列表屬性
  • Json的新方法
  • data自定義數據
  • 延遲加載JS
  • 歷史管理
  • 拖放事件

一些可能感興趣的資源:

http://ued.baidu.com/?p=894

http://www.caniuse.com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章