HTML DOM टेबल createCaption() मेथड एक खाली <caption> एलिमेंट जनरेट करता है और इसे HTML दस्तावेज़ में टेबल में जोड़ता है। यदि टेबल में पहले से कैप्शन है, तो यह मौजूदा कैप्शन को लौटाता है (नया नहीं बनाता)।
सिंटैक्स
object.createCaption()
उदाहरण: जावास्क्रिप्ट से कैप्शन बनाना
<!DOCTYPE html>
<html>
<style>
body { color: #000; background: lightblue; height: 100vh; text-align: center; }
table { margin: 2rem auto; }
caption { color: #db133a; }
.btn { background: #db133a; border: none; height: 2rem; border-radius: 2px; width: 40%; display: block; color: #fff; outline: none; cursor: pointer; margin: 1rem auto; }
.show { font-size: 1.2rem; }
</style>
<body>
<h1>DOM टेबल createCaption() मेथड डेमो</h1>
<table border="2">
<tr>
<td>नाम</td>
<td>रोल नं.</td>
</tr>
<tr>
<td>जॉन</td>
<td>071717</td>
</tr>
<tr>
<td>जेन</td>
<td>031717</td>
</tr>
</table>
<button onclick="create()" class="btn">कैप्शन बनाएं</button>
<script>
function create() {
var tableCaption = document.querySelector('table').createCaption();
tableCaption.innerHTML = "छात्र डेटा";
}
</script>
</body>
</html>
आउटपुट
- प्रारंभ में टेबल बिना कैप्शन के दिखती है
- "कैप्शन बनाएं" बटन पर क्लिक करने के बाद, टेबल के ऊपर "छात्र डेटा" कैप्शन दिखाई देता है
संबंधित मेथड्स
deleteCaption()- मौजूदा कैप्शन हटाता हैcaptionप्रॉपर्टी - कैप्शन एलिमेंट को सीधे एक्सेस करता है
कैप्शन स्टाइलिंग टिप्स
caption {
caption-side: top; /* या bottom */
text-align: center;
font-weight: bold;
padding: 10px;
color: #333;
}