john_woo wrote:
> [...] but how about if I just want one level down of nodes,
> ex
>
> <div id='aaa'>
> <img />
> <div id='bbb'>
> <img />
> </div>
> <img />
> <div/>
The last line should be
</div>
> I want the return of x.getElementsByTagName('img').length = 2 not 3.
>
> How can I get that?
You can *not* get that without successfully redefining gEBTN() or `length'.
However, with XPath it is easily possible to retrieve the number of child
elements of a certain element type:
/*
* yields 2 if `x' is a reference to the element object
* that represents <div id='aaa'>...</div> in the DOM
*/
document.evaluate('img', x, null, 7, null).snapshotLength
http://developer.mozilla.org/en/docs..._in_JavaScript
Without XPath, you will have to iterate through the collection of child
nodes of `x' and test each node:
var count = 0;
for (var i = 0, len = (x.childNodes && x.childNodes.length);
i < len; i++)
{
if (/\s*img\s*/i.test(x.childNodes[i].tagName)) count++;
}
count
http://developer.mozilla.org/en/docs...ent.childNodes
http://developer.mozilla.org/en/docs...Objects:RegExp
http://developer.mozilla.org/en/docs...lement.tagName
Please trim your quotes, especially please do not quote signatures unless
you refer to them directly.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$>