Check if an element is closed using a discrete tag with JavaScript

后端 未结 4 713
感动是毒
感动是毒 2021-01-05 04:47

I am getting the child nodes of en element and i want to check if the tags can actually contain text. For example:


,
4条回答
  •  心在旅途
    2021-01-05 05:10

    use that:

    canContainText: function(node) {
        if(node.nodeType === 3){
            return true;
        }
        if(node.nodeType === 1){
            return /<[^>]+><\/[^>]+>/gi.test(document.createElement(node.nodeName.toLowerCase())).outerHTML;
        }
    }
    

提交回复
热议问题