5 HTML5 new features you don't know, but HTML5 New Features
When you talk about HTML5 in the crowd, you may feel like a foreign dancer or unicorn coming to the center of the room with the obvious "I'm cool, I know what it means. This is not to say that we are vanity. Over the years, basic HTML APIs have not been developed, so that when there is a small new function, such as placeholder, it will make us look new. Although many HTML5 features are implemented in the new browser, most programmers still do not know or have never heard of small and useful APIs. In this article, I will introduce some of these APIs and welcome you to discover more unknown HTML5 APIs!
Element. classList
The classList API provides a basic function for controlling CSS using the JavaScript tool library over the years:
Copy XML/HTML Code to clipboard
- // Add a CSS class
- MyElement. classList. add ("newClass ");
- // Delete a CSS class
- MyElement. classList. remove ("existingClass ");
- // Check whether a CSS class exists
- MyElement. classList. contains ("oneClass ");
- // Reverse the occurrence of a CSS class
- MyElement. classList. toggle ("anotherClass ");
The main value of this new API is simple and practical. Read this article to introduce the features of several other classList functions.
ContextMenu API
This new ContextMenu API is very useful: it does not replace the original right-click menu, but adds your custom right-click menu to the right-click menu of the browser:
Copy XML/HTML Code to clipboard
- <Section contextmenu = "mymenu">
- <! -- Add 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/pai_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>
It should be noted that it is best to use JavaScript to dynamically create these menu codes, because menu events will eventually call JavaScript to execute tasks. if JavaScript is disabled, the right-click menu will not be generated, he does not see the menu at the same time.
Element. dataset
Using dataset APIs, programmers can easily obtain or set data-* Custom Attributes:
Copy XML/HTML Code to clipboard
- /* The following code is used as an example.
- <Div id = "myDiv" data-name = "myDiv" data-id = "myId" data-my-custom-key = "This is the value"> </div>
- */
- // Obtain the element
- Var element = document. getElementById ("myDiv ");
- // Obtain the id
- Var id = element. dataset. id;
- // Read the value of "data-my-custom-key"
- Var customKey = element. dataset. myCustomKey;
- // Modify it to another value
- Element. dataset. myCustomKey = "Some other value ";
- // The result is:
- // <Div id = "myDiv" data-name = "myDiv" data-id = "myId" data-my-custom-key = "Some other value"> </div>
Similar to classList, it is simple and practical.
Window. postMessage API
Even IE8 has supported postMessage API for many years. The postMessage API allows you to transmit information data between two browser windows or iframe:
Copy the content to the clipboard using JavaScript Code
- // Send A message from the window or iframe in Domain A to the window or ifame in Domain B
- Var iframeWindow = document. getElementById ("iframe"). contentWindow;
- IframeWindow. postMessage ("Greetings from the first window! ");
- // Receive messages in a window or iframe In the second domain
- Window. addEventListener ("message", function (event ){
- // Check the validity of the domain
- If (event. origin = "http://www.webhek.com "){
- // Output log information
- Console. log (event. data );
- // Feedback message
- Event. source. postMessage ("are you good! ");
- }
- ]);
The message body can only be a string, but you can use JSON. stringify and JSON. parse to convert the message to a more meaningful data body!
Autofocus attribute
The autofocus attribute enables the BUTTON, INPUT, or TEXTAREA element to automatically become the page focus when the page is loaded:
Copy XML/HTML Code to clipboard
- <Input autofocus = "autofocus"/>
- <Button autofocus = "autofocus"> Hi! </Button>
- <Textarea autofocus = "autofocus"> </textarea>
The autofocus attribute is an ideal feature in a fixed mode like Google search pages.
The browser has slightly different support for each API. Therefore, check the support for these features before use. Take some time to read the detailed descriptions of each API. I believe you will have more discoveries.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.