html
JavaScript में DOM Selectors की शक्ति खोलना
सामग्री तालिका
- परिचय ................................................................. 1
- Document Object Model (DOM) को समझना .................... 3
- JavaScript में DOM Selectors ............................................. 6
- getElementByTagName .............................................. 7
- getElementsByClassName .......................................... 10
- getElementById ..................................................... 13
- व्यावहारिक अनुप्रयोग और उदाहरण ................................... 16
- निष्कर्ष ................................................................... 21
परिचय
JavaScript में DOM Selectors पर इस व्यापक गाइड में आपका स्वागत है। चाहे आप वेब विकास की दुनिया में कदम रख रहे शुरुआती हों या अपने कौशल को सुदृढ़ करने वाले अनुभवी डेवलपर, DOM Selectors को समझना महत्वपूर्ण है। यह eBook वेब पृष्ठों से जानकारी निकालने के तीन मौलिक तरीकों में गहराई से जाता है: getElementByTagName, getElementsByClassName, और getElementById। इन selectors में महारत हासिल करके, आप वेब पृष्ठ तत्वों को गतिशील रूप से संशोधित करने की अपनी क्षमता को बढ़ाएंगे, अधिक इंटरैक्टिव और प्रतिक्रियाशील वेबसाइटों के लिए मार्ग प्रशस्त करेंगे।
DOM Selectors का महत्व
Document Object Model (DOM) आपके वेब पृष्ठों और JavaScript के बीच एक सेतु के रूप में कार्य करता है, जिससे स्क्रिप्ट्स दस्तावेज़ की सामग्री, संरचना और शैली तक गतिशील रूप से पहुंच और अपडेट कर सकते हैं। DOM selectors आपको DOM के भीतर विशिष्ट तत्वों को निर्दिष्ट करने में सक्षम बनाते हैं, जिससे उनके साथ इंटरैक्ट करना आसान हो जाता है। चाहे आप टेक्स्ट बदल रहे हों, शैलियों को संशोधित कर रहे हों, या घटनाओं को संभाल रहे हों, DOM selectors का कुशल उपयोग अनिवार्य है।
फायदे और नुकसान
फायदे | नुकसान |
---|---|
HTML तत्वों के सटीक चयन को सक्षम बनाता है | गलत प्रयोग से बड़े दस्तावेज़ों में प्रदर्शन समस्याएं उत्पन्न हो सकती हैं |
वेब पृष्ठ सामग्री के संशोधन को सरल बनाता है | selectors पर अत्यधिक निर्भरता से कोड कम रखरखाव योग्य हो सकता है |
इंटरैक्टिविटी और गतिशील सामग्री निर्माण को बढ़ाता है | गलत उपयोग से अप्रत्याशित व्यवहार या त्रुटियां हो सकती हैं |
DOM Selectors का उपयोग कब और कहाँ करें
जब आपको निम्न की आवश्यकता होती है, DOM selectors अनिवार्य होते हैं:
- उपयोगकर्ता इंटरैक्शन के आधार पर HTML तत्वों को गतिशील रूप से संशोधित करें।
- Responsive डिज़ाइनों के निर्माण के लिए शैलियों या classes को संशोधित करें।
- प्रक्रियाकरण या सत्यापन के लिए विशेष तत्वों से डेटा प्राप्त करें।
वे सामान्यतः form validation, interactive menus बनाने, और पृष्ठ को पुनः लोड किए बिना सामग्री अपडेट करने जैसे कार्यों में उपयोग किए जाते हैं।
Document Object Model (DOM) को समझना
DOM selectors में गहराई से जाने से पहले, यह समझना महत्वपूर्ण है कि DOM में क्या शामिल है। Document Object Model (DOM) वेब दस्तावेज़ों के लिए एक programming interface है। यह पृष्ठ का प्रतिनिधित्व करता है ताकि प्रोग्राम दस्तावेज़ की संरचना, शैली, और सामग्री को बदल सकें। DOM दस्तावेज़ का एक संरचित प्रतिनिधित्व एक tree of objects के रूप में प्रदान करता है, जिससे इसे नेविगेट और manipulate करना आसान हो जाता है।
मुख्य अवधारणाएँ
- Nodes: DOM tree के building blocks। प्रत्येक element, attribute, और text का एक node होता है।
- Elements: HTML elements जैसे <div>, <p>, और <h1> को element nodes के रूप में दर्शाया जाता है।
- Attributes: HTML elements के attributes (जैसे, id, class) को attribute nodes के रूप में दर्शाया जाता है।
JavaScript में DOM selectors का प्रभावी ढंग से उपयोग करने के लिए इन अवधारणाओं को समझना बुनियादी है।
JavaScript में DOM Selectors
JavaScript DOM elements को select और manipulate करने के लिए कई methods प्रदान करता है। यह सेक्शन तीन मुख्य selectors का पता लगाता है:
- getElementByTagName
- getElementsByClassName
- getElementById
प्रत्येक method के अपने अद्वितीय उपयोग के मामले, फायदे, और सीमाएं हैं।
getElementByTagName
समीक्षा
getElementByTagName method निर्दिष्ट tag name वाले सभी elements को प्राप्त करता है। यह elements का एक HTMLCollection लौटाता है, जो एक live, ordered collection है।
वाक्यविन्यास
1 2 3 |
document.getElementsByTagName(tagName); |
- tagName: खोजने के लिए tag का नाम दर्शाने वाला एक string (जैसे, "div", "p", "h1")।
व्यावहारिक उदाहरण
एक व्यावहारिक उदाहरण के माध्यम से getElementsByTagName का उपयोग कैसे करें, इसका पता लगाते हैं।
HTML संरचना (index.html)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>DOM Selectors Example</title> </head> <body> <h1 id="greeting">Welcome to DOM Selectors</h1> <h2>Subheading Example</h2> <p class="para">This is the first paragraph.</p> <p class="para">This is the second paragraph.</p> </body> </html> |
JavaScript कार्यान्वयन (index.js)
1 2 3 4 5 6 7 8 9 |
// Selecting elements by tag name let elements = document.getElementsByTagName('h2'); console.log(elements); // Outputs HTMLCollection of h2 elements // Accessing the first h2 element let firstH2 = elements[0]; console.log(firstH2.textContent); // Outputs: Subheading Example |
कदम दर कदम व्याख्या
- Selecting Elements:
getElementsByTagName('h2')
method दस्तावेज़ में सभी <h2> तत्वों को प्राप्त करता है। हमारे HTML में, एक <h2> तत्व है। - Understanding HTMLCollection:
लौटाया गया elements variable एक HTMLCollection है, जो DOM तत्वों का एक live संग्रह है। यह ध्यान देना महत्वपूर्ण है कि जबकि आप index (जैसे,elements[0]
) का उपयोग करके elements तक पहुंच सकते हैं, आप सीधे HTMLCollection परforEach
जैसे array methods का उपयोग नहीं कर सकते। - Accessing Specific Elements:
elements[0]
तक पहुंच कर, हम पहले <h2> तत्व को प्राप्त करते हैं।textContent
का उपयोग करके, हम उसका टेक्स्ट लॉग करते हैं: "Subheading Example"।
सीमाएँ
- No Iteration with forEach:
HTMLCollectionsforEach
method को सपोर्ट नहीं करते। इसका उपयोग करने की कोशिश करने पर एक error उत्पन्न होगी।
123456elements.forEach(element => {console.log(element.textContent);});// Error: elements.forEach is not a function - Live Collection:
चूंकि HTMLCollections live हैं, DOM में कोई भी परिवर्तन collection में स्वचालित रूप से प्रतिबिंबित होते हैं। यदि चयन के बाद DOM में संशोधन किया जाता है, तो यह अप्रत्याशित व्यवहार का कारण बन सकता है।
getElementsByClassName
समीक्षा
getElementsByClassName method निर्दिष्ट class name वाले सभी elements को प्राप्त करता है। getElementsByTagName की तरह, यह एक HTMLCollection लौटाता है।
वाक्यविन्यास
1 2 3 |
document.getElementsByClassName(className); |
- className: खोजने के लिए class name को दर्शाने वाला एक string (जैसे, "container", "btn-primary")।
व्यावहारिक उदाहरण
अपने पिछले HTML संरचना के साथ जारी रखते हुए, सभी पैराग्राफ को चुनने के लिए getElementsByClassName का उपयोग करते हैं।
JavaScript कार्यान्वयन (index.js)
1 2 3 4 5 6 7 8 9 |
// Selecting elements by class name let paras = document.getElementsByClassName('para'); console.log(paras); // Outputs HTMLCollection of elements with class 'para' // Accessing the first paragraph let firstPara = paras[0]; console.log(firstPara.textContent); // Outputs: This is the first paragraph. |
कदम दर कदम व्याख्या
- Selecting Elements:
getElementsByClassName('para')
method "para" class name वाले सभी elements को प्राप्त करता है। हमारे HTML में, इस class वाले दो <p> elements हैं। - Accessing Specific Elements:
paras[0]
तक पहुंच कर, हम पहले पैराग्राफ को प्राप्त करते हैं।textContent
का उपयोग करके, हम उसका टेक्स्ट लॉग करते हैं: "This is the first paragraph."
Tag Selectors के ऊपर फायदे
- Specificity:
Class selectors tag selectors की तुलना में अधिक specific targeting की अनुमति देते हैं। उदाहरण के लिए, एक ही class को कई tags साझा कर सकते हैं, जिससे विभिन्न element types में selection संभव होता है। - No Need for Dots in Class Names:
CSS selectors में जहाँ class name से पहले dot (.) होता है (जैसे,.para
), getElementsByClassName method class name को बिना किसी prefix के लेता है।
Multiple Classes को संभालना
यदि एक element में कई classes हैं, तो getElementsByClassName इसे तब भी प्राप्त कर सकता है जब तक कि एक class मेल खाता हो।
1 2 3 |
<p class="para highlight">This is a highlighted paragraph.</p> |
1 2 3 4 |
let highlightedParas = document.getElementsByClassName('highlight'); console.log(highlightedParas); // Outputs HTMLCollection with the highlighted paragraph |
सीमाएँ
- No Iteration with forEach:
getElementsByTagName की तरह, HTMLCollectionsforEach
method को सपोर्ट नहीं करते।
getElementById
समीक्षा
getElementById method unique id attribute के आधार पर एकल element को चुनता है। पिछले methods के विपरीत, यह एक single DOM element लौटाता है न कि एक HTMLCollection।
वाक्यविन्यास
1 2 3 |
document.getElementById(id); |
- id: प्राप्त करने के लिए element के id को दर्शाने वाला एक string (जैसे, "header", "submitBtn")।
व्यावहारिक उदाहरण
एक ही HTML संरचना का उपयोग करके, आइए "greeting" id वाले heading को चुनते हैं।
JavaScript कार्यान्वयन (index.js)
1 2 3 4 5 6 7 8 9 10 11 12 |
// Selecting element by ID let greeting = document.getElementById('greeting'); console.log(greeting); // Outputs the h1 element with id 'greeting' console.log(greeting.textContent); // Outputs: Welcome to DOM Selectors // Attempting to select multiple elements with the same ID document.body.innerHTML += '<h1 id="greeting">Another Greeting</h1>'; let allGreetings = document.getElementById('greeting'); console.log(allGreetings.textContent); // Still outputs the first 'greeting' text |
कदम दर कदम व्याख्या
- Selecting the Element:
getElementById('greeting')
method "greeting" id वाले element को प्राप्त करता है। हमारे HTML में, यह <h1> element है। - Accessing Content:
textContent
का उपयोग करके, हम टेक्स्ट लॉग करते हैं: "Welcome to DOM Selectors"। - Handling Duplicate IDs:
भले ही कई elements एक ही id साझा करते हों (जो अनुशंसित नहीं है), getElementById हमेशा पहली occurrence को लौटाएगा।
Unique IDs का महत्व
- Uniqueness:
id attribute को एक HTML document के भीतर unique होना चाहिए। यह uniqueness सुनिश्चित करता है कि getElementById लगातार इच्छित element को प्राप्त करता है। - CSS and JavaScript Targeting:
Unique IDs सटीक styling और scripting के लिए अनमोल हैं, जिससे बिना अस्पष्टता के targeted interactions संभव होते हैं।
सीमाएँ
- Single Element Selection:
चूंकि IDs unique होने चाहिए, getElementById केवल एक element लौटाता है। यह समान विशेषताओं के आधार पर कई elements को चुनने के लिए उपयुक्त नहीं है। - No Support for Multiple Classes:
class selectors के विपरीत, IDs कई classes या समान समूहों के लिए समर्थन नहीं करते।
व्यावहारिक अनुप्रयोग और उदाहरण
आपकी समझ को मजबूत करने के लिए, आइए एक व्यापक उदाहरण के माध्यम से चलते हैं जो सभी तीन DOM selectors का उपयोग करता है।
प्रोजेक्ट संरचना
- index.html: HTML संरचना शामिल है।
- index.js: DOM manipulation के लिए JavaScript code रखता है।
- styles.css: (Optional) Styling के लिए।
उदाहरण स्थिति
कल्पना करें एक सरल वेब पेज की जिसमें कई पैराग्राफ और headings हों। हम DOM selectors का उपयोग करेंगे:
- विशिष्ट headings के टेक्स्ट को बदलें।
- पैराग्राफ की सामग्री को संशोधित करें।
- उपयोगकर्ता क्रियाओं के आधार पर dynamic interactions को संभालें।
कदम 1: HTML सेटअप करना (index.html)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>DOM Selectors Demo</title> <script defer src="index.js"></script> </head> <body> <h1 id="greeting">Welcome!</h1> <h2>Introduction</h2> <p class="para">This is the first paragraph.</p> <p class="para">This is the second paragraph.</p> <p class="para">This is the third paragraph.</p> <button id="changeContent">Change Content</button> </body> </html> |
कदम 2: JavaScript कार्यान्वयन (index.js)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// Selecting elements by tag name let headings = document.getElementsByTagName('h2'); console.log(headings); // HTMLCollection of h2 elements // Accessing and modifying the first h2 element let firstHeading = headings[0]; firstHeading.textContent = 'Updated Introduction'; console.log(firstHeading.textContent); // Outputs: Updated Introduction // Selecting elements by class name let paragraphs = document.getElementsByClassName('para'); console.log(paragraphs); // HTMLCollection of paragraph elements // Modifying the text of all paragraphs for (let i = 0; i < paragraphs.length; i++) { paragraphs[i].textContent = `This is updated paragraph ${i + 1}.`; } // Selecting element by ID let greeting = document.getElementById('greeting'); console.log(greeting); // h1 element with id 'greeting' // Changing the greeting text greeting.textContent = 'Hello, JavaScript!'; console.log(greeting.textContent); // Outputs: Hello, JavaScript! // Handling button click to reset content let button = document.getElementById('changeContent'); button.addEventListener('click', () => { // Reset headings firstHeading.textContent = 'Introduction'; // Reset paragraphs for (let i = 0; i < paragraphs.length; i++) { paragraphs[i].textContent = `This is the first paragraph.`; } // Reset greeting greeting.textContent = 'Welcome!'; }); |
कदम 3: JavaScript Code की व्याख्या
- Selecting and Modifying <h2> Elements:
- Selection:
getElementsByTagName('h2')
सभी <h2> elements को प्राप्त करता है। - Modification: पहले <h2> element के text को "Updated Introduction" में अपडेट किया जाता है।
- Selection:
- Selecting and Modifying <p> Elements:
- Selection:
getElementsByClassName('para')
सभी "para" class वाले paragraphs को fetch करता है। - Modification: एक
for
loop प्रत्येक paragraph में iterate करता है, इसके text content को इसके क्रम को प्रदर्शित करने के लिए अपडेट करता है।
- Selection:
- Selecting and Modifying the <h1> Element:
- Selection:
getElementById('greeting')
<h1> element को target करता है। - Modification: greeting text को "Hello, JavaScript!" में बदला जाता है।
- Selection:
- Dynamic Interaction with the Button:
- Event Listener: "changeContent" id वाले button पर एक event listener संलग्न किया जाता है।
- Functionality: क्लिक पर, headings, paragraphs, और greeting की मूल सामग्री पुनर्स्थापित कर दी जाती है, जो dynamic DOM manipulation को दर्शाता है।
कदम 4: अपेक्षित आउटपुट
पेज लोड होने पर, शुरुआती सामग्री मूल texts को दर्शाती है। JavaScript के निष्पादन के बाद:
- <h2> heading को "Updated Introduction" में बदल दिया जाता है।
- सभी paragraphs उनके texts को "This is updated paragraph 1.", "This is updated paragraph 2.", आदि में अपडेट कर देते हैं।
- <h1> greeting को "Hello, JavaScript!" में बदल दिया जाता है।
"Change Content" button पर क्लिक करने से सभी परिवर्तनों को उनकी मूल अवस्थाओं में पुनर्स्थापित कर दिया जाता है।
Code Output Diagram
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Before JavaScript Execution: --------------------------------- <h1 id="greeting">Welcome!</h1> <h2>Introduction</h2> <p class="para">This is the first paragraph.</p> <p class="para">This is the second paragraph.</p> <p class="para">This is the third paragraph.</p> <button id="changeContent">Change Content</button> After JavaScript Execution: --------------------------------- <h1 id="greeting">Hello, JavaScript!</h1> <h2>Updated Introduction</h2> <p class="para">This is updated paragraph 1.</p> <p class="para">This is updated paragraph 2.</p> <p class="para">This is updated paragraph 3.</p> <button id="changeContent">Change Content</button> After Button Click: --------------------------------- <h1 id="greeting">Welcome!</h1> <h2>Introduction</h2> <p class="para">This is the first paragraph.</p> <p class="para">This is the second paragraph.</p> <p class="para">This is the third paragraph.</p> <button id="changeContent">Change Content</button> |
निष्कर्ष
DOM selectors में महारत हासिल करना JavaScript के साथ प्रभावी वेब विकास के लिए बुनियादी है। getElementByTagName, getElementsByClassName, और getElementById का लाभ उठाकर, डेवलपर्स HTML elements को सटीक रूप से target और manipulate कर सकते हैं, dynamıc और interactive उपयोगकर्ता अनुभव बना सकते हैं। इन selectors का विवेकपूर्ण उपयोग करने को याद रखें, प्रदर्शन और रखरखाव को ध्यान में रखते हुए। जैसे-जैसे आप अपने projects को build और refine करते हैं, ये DOM manipulation तकनीकें आपके developer toolkit में अनमोल उपकरण बन जाएंगी।
Note: यह लेख AI द्वारा उत्पन्न किया गया है।