js、jq控制class相同当子元素为空时,父元素隐藏

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/jquery.js"></script>
    <style>
        body {
            background: #272727;
            color: #ddd;
            font-family: "open sans", sans-serif;
            font-size: 13px;
            line-height: 1.5;
            margin: 0;
        }
        
        .product-info {
            background: #474747;
            margin: 0 auto 40px;
            padding: 20px;
            width: 800px;
        }
        
        .title {
            font-size: 26px;
            font-weight: 700;
        }
        
        .title h2 {
            line-height: 1;
            margin: 0 0 20px;
        }
    </style>
</head>

<body>
    <div class="product-info prod-spacer">
        <div class="title">
            <h2>This is a Title 1</h2>
        </div>
        <div class="content">
            Remove This Text To See How It Works!!
        </div>
    </div>

    <div class="product-info prod-spacer">
        <div class="title">
            <h2></h2>
        </div>
        <div class="content">
            lk;l
        </div>
    </div>

    <div class="product-info prod-spacer">
        <div class="title">
            <h2>This is a Title 3</h2>
        </div>
        <div class="content">
            Remove This Text To See How It Works!!
        </div>
    </div>
</body>
<script>
    // var $j = jQuery;
    $(function() {
        $('.product-info .title h2').each(function(i) {
            console.log(i)
            if (!$.trim($(this).text()).length) $('.product-info').eq(i).hide();
        })
    });
</script>

</html>
原文地址:https://www.cnblogs.com/520yh/p/13563738.html