免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
你不知道的5個HTML5 API

你不知道的5個HTML5 API

發(fā)表于2012-09-29 10:29| 次閱讀| 來源david walsh blog| 8 條評論| 作者david walsh

摘要:毫無疑問,HTML5已經(jīng)成為當今最流行的一門技術,尤其是Web開發(fā)者們對HTML5的興趣是日趨濃厚。HTML5的許多功能都也能在現(xiàn)代瀏覽器中得以實現(xiàn)。然而,作為開發(fā)者,除了關注HTML5的功能和開發(fā)工具外,你有對其API留意過嗎?本文將為你提供5個你所不知道的API,助你在HTML5開發(fā)這條路上走的更遠。

一提到HTML5,你腦海里是不是閃現(xiàn)這樣的畫面:“一大堆脫衣舞女和獨角獸走進房間,然后演奏著 I’m Sexy and I know it”。產(chǎn)生這樣的想法難道是我們的錯嗎?API的停滯不前,使一些基本的特性,例如使用“占位符”都需要“花一分鐘”。盡管HTML5的許多功能都已經(jīng)在現(xiàn)代瀏覽器中實現(xiàn),但開發(fā)者卻很少關注那些輕量且非常實用的API。本文就將為您“曝光”5個你所不知道的API,并且希望你能探索出更多的HTML5 API,助你在這條路上走的更遠。

Element.classList

classList API提供了一個CSS控制器,而這功能以前都是通過JavaScript實現(xiàn)的:

  1. // Add a class to an element  
  2. myElement.classList.add("newClass");  
  3.  
  4. // Remove a class to an element  
  5. myElement.classList.remove("existingClass");  
  6.  
  7. // Check for existence  
  8. myElement.classList.contains("oneClass");  
  9.  
  10. // Toggle a class  
  11. myElement.classList.toggle("anotherClass"); 

該API最大的優(yōu)點是:簡單和智能,閱讀這篇文章了解更多的classList屬性。

ContextMenu API

ContextMenu API是一個非常出色的菜單API,無需重寫瀏覽器上下文菜單即可把item添加到瀏覽器菜單中,非常簡單、方便。

  1. <section contextmenu="mymenu"> 
  2.   <!--   
  3.     For the purpose of cleanliness,   
  4.     I'll put my menu inside the element that will use it   
  5.   --> 
  6.  
  7.   <!-- add the menu --> 
  8.   <menu type="context" id="mymenu"> 
  9.     <menuitem label="Refresh Post" onclick="window.location.reload();" icon="/images/refresh-icon.png"></menuitem> 
  10.     <menu label="Share on..." icon="/images/share_icon.gif"> 
  11.       <menuitem label="Twitter" icon="/images/twitter_icon.gif" onclick="goTo('//twitter.com/intent/tweet?text=' + document.title + ':  ' + window.location.href);"></menuitem> 
  12.       <menuitem label="Facebook" icon="/images/facebook_icon16x16.gif" onclick="goTo('//facebook.com/sharer/sharer.php?u=' + window.location.href);"></menuitem> 
  13.     </menu> 
  14.   </menu> 
  15. </section> 

備注:最好使用JavaScript創(chuàng)建菜單標記,因為item的動作還是由JavaScript執(zhí)行,如果JavaScript被關閉那么留一個不能執(zhí)行的菜單也沒用。

Element.dataset

dataset這個API可以幫助開發(fā)人員輕松獲取(get)或設置(set)數(shù)據(jù)屬性值:

  1. /*  Assuming element:  
  2.  
  3.   <div id="myDiv" data-name="myDiv" data-id="myId" data-my-custom-key="This is the value"></div> 
  4.  
  5. */  
  6.  
  7. // Get the element  
  8. var element = document.getElementById("myDiv");  
  9.  
  10. // Get the id  
  11. var id = element.dataset.id;  
  12.  
  13. // Retrieves "data-my-custom-key"  
  14. var customKey = element.dataset.myCustomKey;  
  15.  
  16. // Sets the value to something else  
  17. element.dataset.myCustomKey = "Some other value";  
  18.  
  19.   // Element becomes:  
  20.   //    <div id="myDiv" data-name="myDiv" data-id="myId" data-my-custom-key="Some other value"></div>   

無需多說,和classList一樣,簡單有效。

window.postMessage API

IE8很早就開始支持postMessage API,它允許消息在windows和iframe之間發(fā)送:

  1. // From window or frame on domain 1, send a message to the iframe which hosts another domain  
  2. var iframeWindow = document.getElementById("iframe").contentWindow;  
  3. iframeWindow.postMessage("Hello from the first window!");  
  4.  
  5. // From inside the iframe on different host, receive message  
  6. window.addEventListener("message", function(event) {  
  7.   // Make sure we trust the sending domain  
  8.   if(event.origin == "http://davidwalsh.name") {  
  9.     // Log out the message  
  10.     console.log(event.data);  
  11.  
  12.     // Send a message back  
  13.     event.source.postMessage("Hello back!");  
  14.   }  
  15. ]);  

消息只能是字符串類型,使用JSON.stringify和JSON.parse可以解析更多有意義的數(shù)據(jù)。

autofocus Attribute

當頁面完成時,autofocus(自動對焦)屬性可以幫助一個給定的Button、Input或TextArea元素自動獲取焦點。

  1. <input autofocus="autofocus" /> 
  2. <button autofocus="autofocus">Hi!</button> 
  3. <textarea autofocus="autofocus"></textarea> 

每個瀏覽器會支持不同的API,所以在使用之前請認真閱讀使用說明,希望能幫助你更好的使用API。(編譯/張紅月 英文來自:david walsh blog

本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
5個你不知道的HTML5的接口介紹
Html5 常見的新增API1. getElementsByClassName()方法
開發(fā)者必須知道的HTML5十五大新特性
用js原生api代替JQuery api
DOM系列:樣式和類
Android 自定義title 之Action Bar
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服