查找元素是否只包含一个元素,如果为true则隐藏父元素

Finding if a element just holding one element and hide parent element if true

本文关键字:元素 true 隐藏 如果 是否 包含一 查找      更新时间:2023-09-26

我需要一个javascript而不是jquery解决方案。任何帮助都会很棒。我需要检查p标签是否只有br标签,如果是,则隐藏p标签。

<blockquotes> 
<p>
<br> </br> < so if this is the only element then hide the p tag 
</p>
</blockquotes>

试试这个。

function hidePTags() {
    var pTags = document.getElementsByTagName('p');
    for (var i = 0; i < pTags.length; i++) {
        if (pTags[i].children.length == 1 && pTags[i].children[0].tagName == "br") {
            pTags[i].style.visibility = "hidden";
        }
    }
}