<data>-machine-readable data
The <data>
tag is an HTML5 semantic element introduced to associate content with machine-readable data. It is commonly used to embed values or identifiers in a document that can be processed by scripts or backend systems without affecting the visual presentation for users.
Basic Syntax of the <data>
Tag
The syntax of the <data>
tag is very simple, consisting of two main attributes:
value
: A required attribute that defines the machine-readable data value- Tag content: The text content displayed to users
The basic structure is as follows:
<data value="machine-readable-value">Human-readable text</data>
Purpose of the <data>
Tag
The primary role of the <data>
tag is to bridge the gap between human-readable content and machine-readable data in HTML documents. Common use cases include:
- Displaying product information
- Unit conversions
- Storing data identifiers
- Presenting statistical data
Practical Examples
Displaying Product Prices
On e-commerce websites, the <data>
tag can be used to store both the original and display prices:
<p>Special Offer:
<data value="299.00">Only 299 yuan</data>
<small>(Original price: <data value="599.00">599 yuan</data>)</small>
</p>
Temperature Unit Conversion
<p>Current Temperature:
<data value="25" id="temp-celsius">25°C</data>
<button onclick="convertToFahrenheit()">Convert to Fahrenheit</button>
</p>
<script>
function convertToFahrenheit() {
const celsius = document.getElementById('temp-celsius").value;
const fahrenheit = (celsius * 9/5) + 32;
document.getElementById('temp-celsius").textContent = fahrenheit + "°F";
document.getElementById('temp-celsius").value = fahrenheit;
}
</script>
Multilingual Support
<p>
<data value="en_US">English</data> |
<data value="zh_CN">中文</data> |
<data value="ja_JP">日本語</data>
</p>
Differences from Similar Tags
<data>
vs <time>
The <time>
tag is specifically for dates and times, while <data>
is a general-purpose data container:
<!-- Using the time tag for dates -->
<p>Meeting Time: <time datetime="2023-11-15T14:00">2 PM on November 15</time></p>
<!-- Using the data tag for generic data -->
<p>Attendees: <data value="42">Forty-two people</data></p>
<data>
vs <meta>
The <meta>
tag is for document-level metadata and doesn't appear on the page, while the <data>
tag's content is displayed:
<head>
<meta name="author" content="John Doe">
</head>
<body>
<p>Author: <data value="john.doe@example.com">John Doe</data></p>
</body>
Advanced Usage
Combining with Microdata
The <data>
tag can be used with microdata to enhance SEO and machine readability:
<div itemscope itemtype="http://schema.org/Product">
<h2 itemprop="name">Laptop</h2>
<p>Price: <data itemprop="price" value="799.99">$799.99</data></p>
<p>Availability: <data itemprop="availability" value="http://schema.org/InStock">In Stock</data></p>
</div>
Dynamic Data Binding
Use JavaScript to implement dynamic data updates:
<div id="stock-info">
Stock: <data id="stock-count" value="15">15 items</data>
</div>
<script>
function updateStock(count) {
const stockElement = document.getElementById('stock-count');
stockElement.value = count;
stockElement.textContent = count + " items";
stockElement.style.color = count < 5 ? "red" : "green";
}
// Simulate stock changes
setInterval(() => {
const randomChange = Math.floor(Math.random() * 3) - 1; // -1, 0, or 1
const current = parseInt(document.getElementById('stock-count").value);
updateStock(Math.max(0, current + randomChange));
}, 3000);
</script>
Browser Compatibility
The <data>
tag is well-supported in modern browsers, including:
- Chrome 62+
- Firefox 22+
- Safari 7+
- Edge 18+
- Opera 49+
For older browsers that don't support it, the <data>
tag will be displayed as a regular inline element without affecting content presentation.
Accessibility Considerations
While the <data>
tag itself has no special ARIA semantics, it can be combined with other ARIA attributes to enhance accessibility:
<p>
Rating:
<data value="4.5" aria-label="4.5 stars out of 5">
★★★★☆
</data>
</p>
Performance Optimization Tips
- Avoid storing large amounts of data in
<data>
tags - For frequently updated data, consider using
data-*
attributes - When updating multiple
<data>
elements, use document fragments to reduce reflows
// Efficient batch update example
function updateMultipleDataElements(dataMap) {
const fragment = document.createDocumentFragment();
Object.entries(dataMap).forEach(([id, value]) => {
const element = document.createElement('data');
element.id = id;
element.value = value;
element.textContent = value;
fragment.appendChild(element);
});
document.getElementById('container').appendChild(fragment);
}
Integration with Other Technologies
Working with Web Components
<product-card>
<data slot="price" value="29.99">$29.99</data>
<data slot="sku" value="PRD-12345">Product ID: 12345</data>
</product-card>
Usage in Vue.js
<template>
<div v-for="product in products" :key="product.id">
<h3>{{ product.name }}</h3>
<p>Price: <data :value="product.price">{{ formatPrice(product.price) }}</data></p>
</div>
</template>
<script>
export default {
data() {
return {
products: [
{ id: 1, name: 'Mouse', price: 45.99 },
{ id: 2, name: 'Keyboard', price: 89.99 }
]
};
},
methods: {
formatPrice(price) {
return '$' + price.toFixed(2);
}
}
};
</script>
Case Studies
E-commerce Product Listing
<ul class="product-list">
<li class="product-item">
<h3>Wireless Headphones</h3>
<p>Brand: <data value="brand_123">SoundMaster</data></p>
<p>Price: <data value="129.99" class="price">¥129.99</data></p>
<p>Rating: <data value="4.5" class="rating">★★★★☆</data></p>
<data value="in_stock" class="stock-status">In Stock</data>
</li>
<li class="product-item">
<h3>Smart Watch</h3>
<p>Brand: <data value="brand_456">TechWear</data></p>
<p>Price: <data value="259.99" class="price">¥259.99</data></p>
<p>Rating: <data value="4.2" class="rating">★★★★☆</data></p>
<data value="low_stock" class="stock-status">Only 3 left</data>
</li>
</ul>
<style>
.price {
font-weight: bold;
color: #e63946;
}
.stock-status[value="in_stock"] {
color: green;
}
.stock-status[value="low_stock"] {
color: orange;
}
</style>
Academic Paper Statistical Data Display
<article class="research-paper">
<h2>Impact of Climate Change on Agriculture</h2>
<section class="statistics">
<p>Sample Size: <data value="1250">1,250</data> farms</p>
<p>Average Yield Change: <data value="-12.5">12.5% decrease</data></p>
<p>Confidence Interval: <data value="95">95%</data></p>
</section>
</article>
Best Practice Recommendations
- Always provide human-readable content; don't rely solely on the value attribute
- Use standardized data formats for the value attribute
- Consider using JSON format for complex data
- Combine with CSS for conditional styling
- Use
<data>
tags in forms to store additional information
<form id="order-form">
<div class="product-option">
<input type="radio" name="product" id="product1" value="prod_001">
<label for="product1">
Basic - <data value="99.00">$99</data>
<data class="features" value='{"storage":"50GB","users":1}'></data>
</label>
</div>
<div class="product-option">
<input type="radio" name="product" id="product2" value="prod_002">
<label for="product2">
Pro - <data value="199.00">$199</data>
<data class="features" value='{"storage":"200GB","users":5}'></data>
</label>
</div>
</form>
<script>
document.getElementById('order-form').addEventListener('change', function(e) {
if (e.target.name === 'product') {
const features = JSON.parse(e.target.nextElementSibling
.querySelector('.features').value);
updateFeatureDisplay(features);
}
});
</script>
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
下一篇:<ul>-无序列表