jQuery:如何验证某个元素是否为空


【要求】:使用jQuery 如何验证某个元素是否为空

  ♪ 答:

<body>
    <div id="div1">aa</div>
    <input type="input" name="input1" value="bb">
    
    <div id="parent">
    <div></div>
    </div>
    
    <p class="pp">pp</p>
</body>

<script>
    /**
     * 判断是否有内容
     */
    // 方法1:
    var isDivEmpty = $('#div1').text() ? false : true;
    
    // 方法2:
    var isInputEmpty = $('input[name=input1]').val() ? false : true;
    
    /**
     * 判断是否有子元素
     */
    // 方法3:
    console.log( $('#parent').is(':empty') );
    console.log( $('#pp').is(':empty') );
</script>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM