Re: clicking on list elements - problem
Andy LW wrote:
[snip]
[color=blue]
> could you please explain this line (or point to some online source that
> could help me understand [an if statement][/color]
Essentially, the expression gets a reference to the event target, and checks
that this target is a list item (LI) element. I'll walk through it in
detail.
document.onclic k = function(e) {
var tgt;
[color=blue]
> if ((e = e || window.event)[/color]
If you've managed to wade through the silliness, you will hopefully have
grasped that there are basically two event models: Netscape and IE.
The Netscape model passes the event object to a listener as an argument,
whilst IE provides a global object. So, the first thing we need to do is
get this object in one place. The expression
(e = e || window.event)
is fairly simple. The logical OR operator evaluates its operands as
booleans. If the first operand evaluates to true, the /value/ of that
operand will be the result. If the first operand evaluates to false, the
value of the second operand will be the result. In other words, if e is not
an object reference, replace its value with that of window.event.
[color=blue]
> &&[/color]
Before moving on to the next expression, we need to make sure that e
actually contains an event object. To do this, we use the logical AND
operator to ensure that all of the expressions in the if statement evaluate
to true.
[color=blue]
> (tgt = e.srcElement || e.target)[/color]
As part of the difference in event models, both use a different property to
refer to the target of an event. The Netscape/W3C model uses target, whilst
IE uses srcElement. The tgt variable will be assigned one of these
values...
[color=blue]
> && /LI/i.test(tgt.node Name))[/color]
Finally, this regular expression checks the nodeName property of the event
target. The nodeName property contains the tag name for elements (and other
values for other node types). We want to ensure that the event was
targeting a list item (LI), so the expression makes sure that the node name
contains this value.
The regular expression contains the i (case-insensitive) flag. The reason
for this is that the nodeName value can vary depending on whether a
document is HTML or XHTML. With HTML (and XHTML served as HTML), a tag name
(like LI) is always uppercase, and values for other node types (like #text)
are lowercase. With XML-based documents (namely XHTML), the nodeName value
uses the case of the element as XML, unlike SGML, is case-sensitive. This
means that elements borrowed from HTML (like P, TABLE, etc.) are always
lowercase, elements defined in other namespaces may vary. Using a
case-insensitive comparison sidesteps all of this.
I hope that made sense.
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Andy LW wrote:
[snip]
[color=blue]
> could you please explain this line (or point to some online source that
> could help me understand [an if statement][/color]
Essentially, the expression gets a reference to the event target, and checks
that this target is a list item (LI) element. I'll walk through it in
detail.
document.onclic k = function(e) {
var tgt;
[color=blue]
> if ((e = e || window.event)[/color]
If you've managed to wade through the silliness, you will hopefully have
grasped that there are basically two event models: Netscape and IE.
The Netscape model passes the event object to a listener as an argument,
whilst IE provides a global object. So, the first thing we need to do is
get this object in one place. The expression
(e = e || window.event)
is fairly simple. The logical OR operator evaluates its operands as
booleans. If the first operand evaluates to true, the /value/ of that
operand will be the result. If the first operand evaluates to false, the
value of the second operand will be the result. In other words, if e is not
an object reference, replace its value with that of window.event.
[color=blue]
> &&[/color]
Before moving on to the next expression, we need to make sure that e
actually contains an event object. To do this, we use the logical AND
operator to ensure that all of the expressions in the if statement evaluate
to true.
[color=blue]
> (tgt = e.srcElement || e.target)[/color]
As part of the difference in event models, both use a different property to
refer to the target of an event. The Netscape/W3C model uses target, whilst
IE uses srcElement. The tgt variable will be assigned one of these
values...
[color=blue]
> && /LI/i.test(tgt.node Name))[/color]
Finally, this regular expression checks the nodeName property of the event
target. The nodeName property contains the tag name for elements (and other
values for other node types). We want to ensure that the event was
targeting a list item (LI), so the expression makes sure that the node name
contains this value.
The regular expression contains the i (case-insensitive) flag. The reason
for this is that the nodeName value can vary depending on whether a
document is HTML or XHTML. With HTML (and XHTML served as HTML), a tag name
(like LI) is always uppercase, and values for other node types (like #text)
are lowercase. With XML-based documents (namely XHTML), the nodeName value
uses the case of the element as XML, unlike SGML, is case-sensitive. This
means that elements borrowed from HTML (like P, TABLE, etc.) are always
lowercase, elements defined in other namespaces may vary. Using a
case-insensitive comparison sidesteps all of this.
I hope that made sense.
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Comment