Test if an element can contain text

前端 未结 3 1991
灰色年华
灰色年华 2020-12-05 12:02

Is there a clean and robust way in which I can test (using pure javascript or also jQuery) if an HTML element can contain some text?

For instance,

3条回答
  •  难免孤独
    2020-12-05 12:27

    I believe you're looking for a list of void HTML tags:

    The following is a complete list of the void elements in HTML:

    area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr

    From there you would just test the node to see if it is in the list. For example:

    var voidNodeTags = ['AREA', 'BASE', ...];
    var isNodeVoid = function (node) {
        return voidNodeTags.indexOf(node.nodeName) !== -1;
    };
    

    http://jsfiddle.net/3uQjH/

提交回复
热议问题