जिस विधि को आप ढूंढ रहे हैं उसे getElementsByTagName
कहा जाता है। यह तत्वों की एक सरणी जैसी सूची देता है (जो एक सरणी नहीं है)।
ध्यान दें कि आपका अंतिम नमूना .attributes['media']
अन्य विधियों के रूप में एक स्ट्रिंग नहीं लौटाता है। यह इसके बजाय एक विशेषता नोड देता है।
सिद्धांत रूप में सामग्री तक पहुंचने के तरीके समकक्ष होना चाहिए लेकिन ब्राउज़र कीड़े ने वास्तविकता में अन्य व्यवहार को जन्म दिया। लगातार व्यवहार प्राप्त करने के लिए यह एक अमूर्त परत (jQuery जैसी लाइब्रेरी) का उपयोग करना सबसे अच्छा है।यदि आप लाइब्रेरी के बिना प्रोग्राम करना चाहते हैं तो पसंद आपके स्वाद पर निर्भर करती है, हालांकि मैं कहूंगा कि विशेषता नोड के माध्यम से सामान्य रूप से सुरक्षित है।
थोड़ा अधिक तकनीकी विस्तार को जोड़ने के लिए है, हालांकि अलग तरीके से एक ही तरीके समय इस न के बराबर विशेषताओं के लिए जरूरी सच नहीं है का सबसे लौटने। निम्न HTML को उदाहरण के रूप में लें: <a href='test'>
। आप इसे (नीचे उत्पादन फ़ायरफ़ॉक्स से है) a test jsFiddle के दूसरे ब्राउज़र अपने आप कोशिश कर सकते हैं।
// Get reference to element
var a = document.getElementsByTagName('a')[0];
// Existent attributes
console.log(a.href); // String: http://fiddle.jshell.net/_display/test
console.log(a.getAttribute('href')); // String: test
console.log(a.attributes['href']); // Attribute node: href
ध्यान दें कि एक बार एक पूर्ण यूआरआई वापस लौटा दिया गया था, दूसरी बार मूल मूल्य वापस कर दिया गया था।
// Existent invalid attributes
console.log(a.other); // undefined
console.log(a.getAttribute('other')); // String: thing
console.log(a.attributes['other']); // Attribute node: other
पेज लोड पर मौजूद सभी चीजें डीओएम में विलय हो जाती हैं लेकिन अवैध होने पर संपत्ति के रूप में उपलब्ध नहीं होती हैं।
// Inexistent but valid attributes
console.log(a.title); // Empty string
console.log(a.getAttribute('title')); // null
console.log(a.attributes['title']); // undefined
पहली कॉल ने गुणों को डिफ़ॉल्ट मान दिया। फिर हम एक गैर-मौजूद विशेषता के लिए एक मार्कर के रूप null
देखा। आखिरकार हमें एक तथाकथित नामांकित नोडमैप मिला जो एक सरणी और वस्तु के मिश्रण की तरह कुछ है। किसी ऑब्जेक्ट के रूप में इसे एक्सेस करने से undefined
मान दिया गया।
// Creating attributes
a.setAttribute('title', 'test title');
console.log(a.title); // String: test title
console.log(a.getAttribute('title')); // String: test title
console.log(a.attributes['title']); // Attribute node: title
गुण भी संपत्ति के रूप में उपलब्ध हो जाता है। एक मान्य विशेषता के लिए
// Creating "attributes" by using property
a.rel = 'test rel';
console.log(a.rel); // String: test rel
console.log(a.getAttribute('rel')); // String: test rel
console.log(a.attributes['rel']); // Attribute node: rel
स्थापना संपत्ति भी attributes
मानचित्र में एक प्रविष्टि बनाता है। a
पर
// Inexistent invalid attributes
console.log(a.dummyInvention); // undefined
console.log(a.getAttribute('dummyInvention')); // null
console.log(a.attributes['dummyInvention']); // undefined
संपत्ति का उपयोग, मार्कर वापसी मान और नोड नक्शे पर सूचकांक का उपयोग कर सकते।
// Creating invalid attributes via setAttribute
a.setAttribute('title2', 'test title2');
console.log(a.title2); // undefined
console.log(a.getAttribute('title2')); // String: test title2
console.log(a.attributes['title2']); // Attribute node: title2
गुण बनाया जाता है, भले ही इसके विद्यमान अमान्य है, लेकिन यह संपत्ति के रूप में उपलब्ध नहीं है।
// Creating invalid "attributes" via property
a.title3 = 'test title3';
console.log(a.title3); // String: test title3
console.log(a.getAttribute('title3')); // null
console.log(a.attributes['title3']); // undefined
वस्तु a
बढ़ाया है लेकिन डोम अछूता है।
// NamedNodeMap of length 4 and indexes other, href, title, rel, title2 (valid attributes or result of setAttribute in order of creation except those from parsing)
console.log(a.attributes);
नोड मानचित्र केवल डोम की वर्तमान स्थिति को दर्शाता है। इससे हमारे वस्तु a
कि हम getElementsByTagName
के माध्यम से प्राप्त करने के लिए विस्तार के बारे में पता नहीं है।
यह ध्यान रखें कि जावास्क्रिप्ट वस्तु से छेड़छाड़ जरूरी डोम को प्रभावित नहीं करता महत्वपूर्ण है। डीओएम केवल उन चीज़ों को प्रतिबिंबित करता है जो डीओएम विधियों या संपत्ति संशोधनों (पूर्वनिर्धारित गुणों के साथ) के साथ पार्सिंग प्लस संशोधन पर उपलब्ध हैं। मुझे उम्मीद है कि मुझे किसी भी महत्वपूर्ण मामले को याद नहीं आया है और यह देखने के लिए टिप्पणियां पर्याप्त हैं कि क्या होता है।
मैं अंतिम नामांकित नोडमैप पर एक टिप्पणी की सराहना करता हूं क्योंकि मैं जानना चाहता हूं कि पार्सिंग से गुणों के क्रम को त्यागने के लिए फ़ायरफ़ॉक्स का व्यवहार सही है या नहीं।
बस एक नोट: जावास्क्रिप्ट ('.x = [" x "] सामान्य रूप से एक ही चीज़ के लिए पहले दो संकलन, यह तत्व विशेषताओं के लिए विशिष्ट नहीं है। –
यह 'getElementsByTagName', नहीं' getElementByTag' है। – Oded
संभावित डुप्लिकेट [getAttribute() का उपयोग करने के लिए, या getAttribute() का उपयोग न करने के लिए: यह सवाल है] (http://stackoverflow.com/questions/7278922/to-use-getattribute-or-not-to-use -getattribute-that-the-question) –