html5中几个不容错过的api或者tips小结

所属分类: 网页制作 / 应用技巧 阅读数: 855
收藏 0 赞 0 分享
之前的博文中,一直有关注一些HTML 5中的值得关注但少用的API或者tips,这次继续小结一些。
1)element.classList
详细的可以参考
https://developer.mozilla.org/en-US/docs/DOM/element.classList
这里简单说下,它其实是一个快速对某个元素的class进行操作的新的DOM API了,比如
包括了add,remove,toggle,contains的方法,比如
myDiv.classList.add('myCssClass');
myDiv.classList.remove('myCssClass');
myDiv.classList.toggle('myCssClass'); //now it's added
myDiv.classList.toggle('myCssClass'); //now it's removed
myDiv.classList.contains('myCssClass'); //returns true or false
现在的浏览器支持情况为:
chrome 8.0+,ie 10,opera 11.5,safari 5.1
2)ContextMenu 上下文菜单 API
这个API是HTML 5的,用来可以生成简单的可以点击的上下文菜单,能给用户快速的选择和显示,比如

复制代码
代码如下:

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

3)element.dataset
这个API用来获得键值对的时候很有用:
比如:

复制代码
代码如下:

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

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

复制代码
代码如下:

// 获得元素
var element = document.getElementById("myDiv");
// 获得id
var id = element.dataset.id;
// 获得data-my-custom-key"
var customKey = element.dataset.myCustomKey;
// 设置新的值
element.dataset.myCustomKey = "Some other value";

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

复制代码
代码如下:

// From window or frame on domain 1, send a message to the iframe which hosts another domain
var iframeWindow = document.getElementById("iframe").contentWindow;
iframeWindow.postMessage("Hello from the first window!");
// From inside the iframe on different host, receive message
window.addEventListener("message", function(event) {
if(event.origin == "http://davidwalsh.name") {
// Log out the message
console.log(event.data);
// Send a message back
event.source.postMessage("Hello back!");
}
]);

5)autofocus
这个很简单了,自动focus到控件

复制代码
代码如下:

<input autofocus="autofocus" />
<button autofocus="autofocus">Hi!</button>
<textarea autofocus="autofocus"></textarea>
更多精彩内容其他人还在看

CSS中浏览器对尺寸和宽高解释差异的解决方法

不同的浏览器对margin、padding、height、width 等属性的解释有很大的偏差。有时我们发现同样的两列布局,在不同的浏览器中两列长短不一,类似的情况太多了,如此困扰大家的麻烦,如何解决?
收藏 0 赞 0 分享

网页变灰的笔记 细节问题处理

grayscale.js这个文件是用来兼容各个浏览器的,在一般情况下,使用它绝对不会有问题,但是在实际的操作中,还是遇到了一些麻烦。
收藏 0 赞 0 分享

页面中marquee与flash同时存在时的冲突解决方案

冲突的症状主要表现在FLASH按钮一直跳动,不稳定,影响网页美观及正常访问。
收藏 0 赞 0 分享

提高网站访问速度的六种方法

以下6种优化方法都是前端的,前端优化的意义在于减少http请求,减少网站前端程序组成的体积。
收藏 0 赞 0 分享

新鲜出炉的有用图标集25组 国外下载

恰当的图标(ICON)能使网站更具活力 曾几何时,现在的网站图标大同小异,我们的眼睛早已见惯不惊。于是,这里收集了25套专业设计的全新的图标集,希望对你有用!
收藏 0 赞 0 分享

新手建站教程 十天学会做网站

十天学会做网站教程采用最通俗易懂的语言、从最基本的开始,带领大家开始建站。教程由许耀鹏历时半年时间写成。是非常好的新手建站入门教程。
收藏 0 赞 0 分享

新手建站入门教程 建站需要的条件和工具

很多新手在论坛都会说,我从来没做过网站,做网站需要些什么条件啊,会不会很难啊?
收藏 0 赞 0 分享

新手建站入门教程 域名的解析与绑定

昨天我们讲到了建立一个属于自己的网站需要的条件和工具,里面讲到首先需要注册一个属于自己的域名,和需要购买一个虚拟主机。(详情请看:新手建站第一帖:建站需要的条件和工具)
收藏 0 赞 0 分享

新手建站入门教程③ 别名(CNAME)记录和URL转发

在上一帖中为大家介绍了域名的解析和A记录解析的方法,今天我就为大家讲解一下另一种解析方法:别名(CNAME)记录。同时为大家讲解下URL转发的好处和方法。
收藏 0 赞 0 分享

新手建站入门教程④:如何绑定子目录

去买空间的时候,我想很多朋友都已经注意到了,有的空间介绍上会有类似于“支持绑定1个子目录”、“赠送1个子目录”等词。那么这些介绍是什么意思呢?
收藏 0 赞 0 分享
查看更多