JS का DOM
In this page:
document.getElementById("id");
document.querySelector("selector");
element.textContent = value;
element.style.property = value;
DOM क्या है
DOM किसी HTML page का एक live, in-memory tree representation है, जहाँ हर element, attribute, और text का टुकड़ा एक connected node बन जाता है जिसे JavaScript सीधे पढ़ और बदल सकता है।
उदाहरण: What Is the DOM
console.log(document); // the live in-memory tree of the page
document Object
document पूरे loaded page को represent करने वाला built-in object है, जो लगभग हर DOM operation के लिए शुरुआती बिंदु का काम करता है, elements ढूंढना, properties पढ़ना, या नया content बनाना।
उदाहरण: The document Object
console.log(document.title);
console.log(document.body);
DOM Tree Structure
DOM किसी page को hierarchically organize करता है, सबसे ऊपर document, उसके बाद html, फिर head और body, और हर nested element उसी parent-child relationship को individual text content तक आगे बढ़ाता है।
उदाहरण: The DOM Tree Structure
// document -> html -> head/body -> nested elements -> text
console.log(document.documentElement.tagName); // "HTML"
Elements को Select करना (Preview)
किसी element को manipulate करने से पहले, आपको पहले उसे DOM से select करना होगा, getElementById() या querySelector() जैसे methods से, जिन्हें अगले chapter में DOM methods पर पूरी गहराई से कवर किया गया है।
उदाहरण: Selecting Elements (Preview)
const el = document.getElementById("demo"); // preview of the next chapter
console.log(el);
DOM Manipulation की बुनियादी बातें
किसी element के select हो जाने के बाद, JavaScript उसके text, attributes, styles, या structure को सीधे बदल सकता है, और ये बदलाव बिना किसी reload के तुरंत page पर दिखाई देते हैं, यही interactive web pages की बुनियाद है।
उदाहरण: DOM Manipulation Basics
document.body.style.color = "blue";
document.title = "Updated Title";
- page के DOM elements को पूरी तरह load करने से पहले access करने की कोशिश करना, जिससे उम्मीद किए गए element के बजाय null मिलता है।
- DOM tree को original HTML source से confuse करना, DOM को JavaScript से modify किया जा सकता है और अब यह initial HTML से exactly match नहीं करता।
- यह मान लेना कि हर visible change को page reload चाहिए, DOM manipulation किसी reload के बिना screen पर मौजूद चीज़ को instantly update कर देता है।
- DOM किसी HTML page को connected node objects के एक tree की तरह represent करता है जिसे JavaScript पढ़ और modify कर सकता है।
- document object पूरे DOM tree को access और उससे interact करने का entry point है।
- DOM manipulation, JavaScript से text, attributes, या structure बदलना, वही है जो pages को dynamic और interactive महसूस कराता है।
DOM और document object core, universal web platform features हैं जो हर browser में समान रूप से supported हैं।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic