首页 > 基础设施 > 正文

HTML 5中几个值得关注的API

2012-10-11 11:09:34  来源:IT168

摘要:HTML 5最近两年一直受到人们的关注,而且对于HTML 5也有许多值得关注的API或都技巧,本文例举了HTML 5的五个API,一起来看一下。
关键词: HTML 5 API

    之前一直有关注一些HTML 5中的值得关注但少用的API或者技巧,这里给大家总结了一些,希望可以在日常工作中给大家提供指导和借鉴作用。


    1.element.classList


    这里简单说下,它其实是一个快速对某个元素的class进行操作的新的DOM API了,比如包括了add,remove,toggle,contains的方法,比如:


    1 myDiv.classList.add(‘myCssClass’);


    2 myDiv.classList.remove(‘myCssClass’);


    3 myDiv.classList.toggle(‘myCssClass’); //now it's added


    4 myDiv.classList.toggle(‘myCssClass’); //now it's removed


    5 myDiv.classList.contains(‘myCssClass’); //returns true or false


    现在的浏览器支持情况为:


    chrome 8.0+,ie 10,opera 11.5,safari 5.1


    2.ContextMenu上下文菜单API


    这个APIHTML 5的,用来可以生成简单的可以点击的上下文菜单,能给用户快速的选择和显示,比如:


    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>[page]
    3.element.dataset


    这个API用来获得键值对的时候很有用:


    比如:


    1 <div id=“myDiv” data-name=“myDiv” data-id=“myId” data-my-custom-key=“This is the value”></div>


    则通过下面这些可以获得键值对,这个用在jquery mobile中很实用:


    1 // 获得元素


    2 var element = document.getElementById(“myDiv”);


    3


    4 // 获得id


    5 var id = element.dataset.id;


    6


    7 // 获得data-my-custom-key“


    8 var customKey = element.dataset.myCustomKey;


    9


    10 // 设置新的值


    11 element.dataset.myCustomKey = ”Some other value“;


    4.postMessage API


    这个居然在IE 8后就支持了,可以支持在不同domain的iframe中传递消息。


    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         if(event.origin == ”http://davidwalsh.name“) {


    8         // Log out the message


    9         console.log(event.data);


    10


    11         // Send a message back


    12         event.source.postMessage(”Hello back!“);


    13     }


    14 ]);


    5.autofocus


    这个很简单了,自动focus到控件。


    1 <input autofocus=”autofocus“ />


    2 <button autofocus=”autofocus“>Hi!</button>


    3 <textarea autofocus=”autofocus“></textarea>


第三十四届CIO班招生
国际CIO认证培训
首席数据官(CDO)认证培训
责编:zhangyexi

免责声明:本网站(http://www.ciotimes.com/)内容主要来自原创、合作媒体供稿和第三方投稿,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证有关资料的准确性及可靠性,读者在使用前请进一步核实,并对任何自主决定的行为负责。本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。
本网站刊载的所有内容(包括但不仅限文字、图片、LOGO、音频、视频、软件、程序等)版权归原作者所有。任何单位或个人认为本网站中的内容可能涉嫌侵犯其知识产权或存在不实内容时,请及时通知本站,予以删除。