Summary of several APIs or tips in HTML5 that cannot be missed

Summary of several APIs or tips in HTML5 that cannot be missed
In previous blog posts, I have been focusing on some of the less-used but noteworthy APIs or tips in HTML 5. This time I will continue to summarize some of them.
1)element.classList
For details, please refer to
https://developer.mozilla.org/en-US/docs/DOM/element.classList
Here is a brief introduction. It is actually a new DOM API for quickly operating on the class of an element, including methods such as add, remove, toggle, and 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
Current browser support is:
Chrome 8.0+, IE 10, Opera 11.5, Safari 5.1
2)ContextMenu context menu API
This API is for HTML 5 and is used to generate simple clickable context menus that give users quick selection and display, such as

Copy code
The code is as follows:

<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
This API is useful for getting key-value pairs:
for example:

Copy code
The code is as follows:

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

Then you can get the key-value pairs through the following, which is very useful in jQuery Mobile:

Copy code
The code is as follows:

// Get the element
var element = document.getElementById("myDiv");
// Get id
var id = element.dataset.id;
// Get data-my-custom-key"
var customKey = element.dataset.myCustomKey;
// Set the new value
element.dataset.myCustomKey = "Some other value";

4) postMessage API
This is actually supported after IE 8, which can support message delivery in iframes of different domains.

Copy code
The code is as follows:

// 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
This is very simple, automatically focus to the control

Copy code
The code is as follows:

<input autofocus="autofocus" />
<button autofocus="autofocus">Hi!</button>
<textarea autofocus="autofocus"></textarea>

<<:  Practice of using SuperMap in Vue

>>:  Sample code for CSS dynamic loading bar effect

Recommend

Vue implements a simple shopping cart example

This article shares the specific code of Vue to i...

Vue implements the digital thousands separator format globally

This article example shares the specific code for...

What do CN2, GIA, CIA, BGP and IPLC mean?

What is CN2 line? CN2 stands for China Telecom Ne...

Vue implements dynamic circular percentage progress bar

Recently, when developing a small program, I enco...

A good way to improve your design skills

So-called talent (left brain and right brain) Tha...

Four completely different experiences in Apple Watch interaction design revealed

Today is still a case of Watch app design. I love...

MySQL table addition, deletion, modification and query basic tutorial

1. Create insert into [table name] (field1, field...

Example of how to optimize MySQL insert performance

MySQL Performance Optimization MySQL performance ...

Detailed explanation of the usage of Object.assign() in ES6

Table of contents 2. Purpose 2.1 Adding propertie...

Install two MySQL5.6.35 databases under win10

Record the installation of two MySQL5.6.35 databa...

Why does your height:100% not work?

Why doesn't your height:100% work? This knowl...

Use of environment variables in Docker and solutions to common problems

Preface Docker can configure environment variable...