开发者

Exclude certain TagNames from * selection

开发者 https://www.devze.com 2023-03-10 06:16 出处:网络
Using the statement: var children = document.getElementById(\'id\').getElementsByTagN开发者_StackOverflow社区ame(\'*\');

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号