परिचय
हम data-* एट्रिब्यूट्स का उपयोग करके एलिमेंट्स के बारे में अतिरिक्त जानकारी संग्रहीत कर सकते हैं। ये कस्टम डेटा एट्रिब्यूट्स CSS सेलेक्टर्स (attr() फंक्शन के साथ) और जावास्क्रिप्ट दोनों के लिए सुलभ हैं।
उदाहरण 1: जावास्क्रिप्ट इंटरैक्शन के लिए डेटा एट्रिब्यूट्स
<!DOCTYPE html>
<html>
<head>
<style>
dl {
margin: 2%;
}
p {
width: 60%;
background-color: lightgreen;
padding: 2%;
color: white;
text-align: center;
}
dt {
font-weight: bold;
}
dt:hover {
cursor: pointer;
}
dd {
font-style: italic;
}
</style>
</head>
<body>
<dl>
<dt onmouseover="showDescription(this)" data-food-type="beverages">Tea</dt>
<dd>Hot Spicy Tea or Ice Lemon Tea </dd>
<dt onmouseover="showDescription(this)" data-food-type="snacks">Toast</dt>
<dd>Hot Garlic Butter Toast</dd>
</dl>
<p>(hover over food item)</p>
</body>
<script>
function showDescription(food) {
let foodType = food.getAttribute("data-food-type");
document.querySelector('p').textContent = ("We have " + food.innerHTML + " in " + foodType + ".");
}
</script>
</html>
उदाहरण 2: CSS attr() के साथ डेटा एट्रिब्यूट्स
<!DOCTYPE html>
<html>
<head>
<style>
section {
margin: 8%;
box-shadow: inset 0 0 20px red;
width: 300px;
padding: 2%;
}
section[data-number='77'] {
height: 120px;
border-radius: 15px;
}
section::before {
content: attr(data-user);
font-size: 1.2em;
}
section::after {
content: attr(data-number);
}
</style>
</head>
<body>
<section data-number="77" data-user="Client">
<p>Demo Text</p>
</section>
</body>
</html>
डेटा एट्रिब्यूट्स के लिए CSS सेलेक्टर्स
| सेलेक्टर | विवरण |
|---|---|
[data-foo] |
data-foo एट्रिब्यूट वाले एलिमेंट्स का चयन करता है |
[data-foo="bar"] |
data-foo="bar" वाले एलिमेंट्स का चयन करता है |
[data-foo^="bar"] |
ऐसे एलिमेंट्स जिनका data-foo "bar" से शुरू होता है |
[data-foo$="bar"] |
ऐसे एलिमेंट्स जिनका data-foo "bar" पर समाप्त होता है |
[data-foo*="bar"] |
ऐसे एलिमेंट्स जिनके data-foo में "bar" कहीं भी होता है |
व्यावहारिक उपयोग के मामले
1. CSS-ओनली टूलटिप्स
<!DOCTYPE html>
<html>
<head>
<style>
.tooltip {
position: relative;
border-bottom: 1px dotted #333;
}
.tooltip::after {
content: attr(data-tooltip);
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
padding: 0.5rem 1rem;
background: #333;
color: white;
border-radius: 4px;
white-space: nowrap;
opacity: 0;
visibility: hidden;
transition: opacity 0.2s, visibility 0.2s;
}
.tooltip:hover::after {
opacity: 1;
visibility: visible;
}
</style>
</head>
<body>
<p> <span class="tooltip" data-tooltip="यह एक टूलटिप है">मुझ पर होवर करें</span> अधिक जानकारी के लिए।</p>
</body>
</html>
2. डेटा-ड्रिवन स्टाइलिंग
<!DOCTYPE html>
<html>
<head>
<style>
.status-badge {
display: inline-block;
padding: 0.25rem 0.75rem;
border-radius: 9999px;
font-size: 0.875rem;
font-weight: 500;
}
.status-badge[data-status="active"] { background: #d4edda; color: #155724; }
.status-badge[data-status="pending"] { background: #fff3cd; color: #856404; }
.status-badge[data-status="inactive"] { background: #f8d7da; color: #721c24; }
</style>
</head>
<body>
<span class="status-badge" data-status="active">सक्रिय</span>
<span class="status-badge" data-status="pending">लंबित</span>
<span class="status-badge" data-status="inactive">निष्क्रिय</span>
</body>
</html>
बेस्ट प्रैक्टिसेज
data-से शुरू होने वाले वर्णनात्मक नामों का उपयोग करें (जैसेdata-user-id,data-api-endpoint)- संवेदनशील डेटा (टोकन, PII) संग्रहीत न करें — यह DOM में दृश्यमान है
- कॉन्फिगरेशन/स्टेट के लिए उपयोग करें जिनके लिए फुल डेटा एट्रिब्यूट API की आवश्यकता नहीं है
- जावास्क्रिप्ट में,
element.dataset.propertyNameका उपयोग करें (कैमलकेस में परिवर्तित:data-user-id→dataset.userId)