Re: can DIV elements be reached?
- From: Thomas 'PointedEars' Lahn <PointedEars@xxxxxx>
- Date: Mon, 23 Mar 2009 00:22:11 +0100
David Deley wrote:
I came across this quote:
"There is no legal way to use the name attribute from such tags as
div or span, according to the W3C HTML 4.01 specs."
`div' (or DIV, in HTML) and `span' (or SPAN, in HTML), are *elements*, not
tags (elements consist of [sometimes optional] tags, and [sometimes
optional] content[1]). What they probably wanted to say is that those
elements have no `name' attribute per Specification[2], so you SHOULD NOT
rely on retrieving the value of that document.getElementsByName() works on them.
[1] <http://www.w3.org/TR/html401/intro/sgmltut.html#h-3.2.1>
[2] <http://www.w3.org/TR/html401/struct/global.html#edef-DIV>
but in the next paragraph they say:http://www.aglasshalffull.org/css-resources/Using-getElementsByTagName-and-getElementsByName.htm
"We’ll be using getElementsByTagName for getting all divs".
[...]
Ref:
Because `div' (and `span') elements have no `name' attribute declared in the
(X)HTML DTDs, another, more reliable way is required to retrieve references
to the relevant DOM element objects. A possibility on that way certainly is
document.getElementsByTagName(). What they neglected to recognize, to put
it politely, though, is that the markup they are operating on is not Valid
to begin with.
So now I'm confused.
Are they saying it's not legal, but we can do it anyway? Or what?
Quote:
| function hue(nextColor){
| var e=document.getElementsByName("n");
| for(var i=0;i<e.length;i++){e[i].style.backgroundColor = nextColor;}
| var e=document.getElementsByTagName("div");
| for(var i=0;i<e.length;i++){e[i].style.backgroundColor = nextColor;}
| }
| [...]
| <div name="m" style="top:270px;"><a HREF="#" onfocus="this.blur();wipe()"
| onMouseOver="wipe(); return true"
| onClick="javascript:hue('coral')"> coral
| (click) </a></div>
|
| <div name="m" style="top:323px;"><a HREF="#" onfocus="this.blur();wipe()"
| onMouseOver="javascript:hue('lavender')"> lavender
| (hover) </a></div>
They show a solution that is only half-way correct (and barely
maintainable), if that (note also the pointless occurences of `javascript:',
accumulations of ` ' to make space, and CSS `top' property declarations
without adjacent `position' declarations which are sure indicators of
cluelessness).
For the HTML markup operated on MUST NOT contain `div' elements (or `span'
elements) that have a `name' attribute specified in the first place.
Therefore, a fully correct solution MUST NOT have that attribute there nor
should it rely on retrieving the value of the value of that attribute in the
first place; instead, the following solutions come to mind:
A) Make the relevant `div' elements child elements of another element that
has an ID. Refer to that parent element object by ID, and use its
getElementsByTagName() method (or iterate through its childNodes
collection, observing that white-space may create white-space text
nodes to consider).
B) (which I prefer and have implemented on occasion, because it is more
compatible and probably more efficient than A)
Let the relevant `div' elements have a different ID each, that consists
of a common prefix followed by an integer counter. In a loop, access
each element by ID and loop index:
var o, prefix = "foo", i = 0;
while ((o = document.getElementById(prefix + (++i)))
{
// work with `o'
}
So much for their "dispelling misinformation in a few books and on the
Internet" and their "confining all [their] nuggets of wisdom to the four
areas [they] know something about: JavaScript, CSS, HTML, and DHTML".
Avoid that wannabe site like the plague.
PointedEars
.
- Follow-Ups:
- Re: can DIV elements be reached?
- From: Thomas 'PointedEars' Lahn
- Re: can DIV elements be reached?
- References:
- can DIV elements be reached?
- From: David Deley
- can DIV elements be reached?
- Prev by Date: How to break/move to new page?
- Next by Date: third program can't find why it won't work.
- Previous by thread: Re: can DIV elements be reached?
- Next by thread: Re: can DIV elements be reached?
- Index(es):
Relevant Pages
|
Loading