HTML5 Customizing the Data property

Source: Internet
Author: User

Perhaps when you use jquery mobile, you will often see the use of Data-role, Data-theme, and so on, such as: The following code to achieve the effect of the header:

[HTML]
<div data-role= "Header" >
</div>

Why write a data-role= "header" can be achieved at the bottom of the black, Text center display effect?


This article provides one of the simplest ways to achieve this by giving you an intuitive understanding of these usages.


We write an HTML page, customize a data-chb= "header" property, want to have this attribute of the div area background color is black, the text is white, center display; the div with no Data-chb custom attributes is displayed by default, with the HTML code as follows:

[HTML]
<body>
<div data-chb= "Header" >
</div>
<br/>
<div>
I do not use DATA-CHB custom attributes, how to show how to show;
</div>
</body>

To achieve the "background color is black, text is white, centered display" effect, we define the following CSS:
[CSS]
<style>
. ui_header {

Text-align:center;
Color:white;
border:1px solid #000;
}
</style>

Then we implement the following JS method to dynamically add CSS definitions to the page load, changing the display style of the div with the Data-chb attribute:
[JavaScript]
<script type= "Text/javascript" >
Window.onload=function () {
var elems = document.getelementsbytagname ("div");
if (elems!=null&&elems.length>0) {
var length = Elems.length;
Traverse All div controls
for (Var i=0;i<length;i++) {
var elem = elems[i];
Gets the custom properties of the control
var customattr = Elem.dataset.chb;
You can also get custom properties in the following ways
var customattr = elem.dataset["ChB"];
If it is our predefined header value, it means that we need to handle
if (customattr== "header") {
Add Style
Elem.setattribute ("Class", "Ui_header");
}
}
}
}
</script>

HTML5 Customizing the Data property

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.