Detect inline/block type of a DOM element

后端 未结 3 1662
Happy的楠姐
Happy的楠姐 2020-12-08 20:27

How to detect whether a DOM element is block or inline with javascript?

For example, is there a function/property which returns \'inline\' for a \'

3条回答
  •  轮回少年
    2020-12-08 21:09

    The traditional and rather ugly way of doing this is to consult a list of element names for block-level elements:

    var blockRegex = /^(address|blockquote|body|center|dir|div|dl|fieldset|form|h[1-6]|hr|isindex|menu|noframes|noscript|ol|p|pre|table|ul|dd|dt|frameset|li|tbody|td|tfoot|th|thead|tr|html)$/i;
    
    function isBlockLevel(el) {
        return blockRegex.test(el.nodeName);
    }
    

提交回复
热议问题