Exclude certain TagNames from * selection
Using the statement:
var children = document.getElementById('id').getElementsByTagN开发者_StackOverflow社区ame('*');
I'd like to exclude all <BR> elements, is there a syntax for getElementsByTagName that lets me do that, or some other nice way?
You can't do it with a native function, but you can easily filter.
http://jsfiddle.net/idbentley/ncH95/4/
It would be easier to use jQuery or a similar library (Zepto is a good tiny lib), but if you want to use raw javascript you can use the above.
If you use an framework like jquery you can do
$('#id').children().not('br');
With jQuery you could do:
$('#id *').not('br');
There isn't a special operator for that, but you can filter easily.
(function () {
var element = document.getElementById('id');
if (element.tagName === "br")
throw "AHHHHH!!! IT'S A BR AHHHHH!!!!!!";
// Do stuff if it's not a br.
}).apply(this);
精彩评论