为何W3C文档中的document.tags语法无法生效?与getElementsByTagName对比
Great question—this is a classic case of mixing up early browser-specific DOM extensions with standardized W3C APIs. Let’s break down what’s going on:
1. document.tags is a long-deprecated, non-standard shortcut
The document.tags syntax you found was a private browser extension supported by ancient browsers like Netscape Navigator back in the early days of JavaScript. It was never part of any official W3C DOM standard—even if some early W3C documents might have referenced existing browser implementations of the time.
Modern browsers have completely phased out these non-standard shortcuts to align strictly with W3C and WHATWG DOM specs. Today, your browser doesn’t even recognize the document.tags property, so running that line of code does nothing at all.
2. Your working code uses standard, widely supported DOM APIs
The code you’re familiar with:
document.getElementsByTagName('h1').item(0).style.color = 'blue'
follows the official W3C DOM specification step-by-step:
getElementsByTagName('h1')returns a live HTMLCollection of all<h1>elements (a standard method).item(0)retrieves the first element in that collection (the standard way to access items in a collection).style.color = 'blue'modifies the element’s inline CSS style (the standard approach to manipulating element styles via DOM)
3. A modern, more concise alternative
If you want a shorter standard syntax, you can use querySelector (supported in all modern browsers):
document.querySelector('h1').style.color = 'blue'
querySelector lets you use CSS selector syntax to target elements directly, making it more flexible than getElementsByTagName for complex selections.
内容的提问来源于stack exchange,提问作者Patrik

