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

后端 未结 4 731
感动是毒
感动是毒 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条回答
  •  萌比男神i
    2021-01-05 05:20

    Following script works just fine (cross-browser issue resolved):

    function containTxt(tag) {
        var tags = /^(img|video)$/i; // any values which will be in `tags` set will be treated as they can't have a text value
        return !tags.test(tag);
    }
    
    console.log(containTxt("img")); // returns false
    console.log(containTxt("div")); // returns true
    

提交回复
热议问题