Using an if statement to check if a div is empty

前端 未结 10 1131
忘了有多久
忘了有多久 2020-11-28 03:20

I\'m trying to remove a specific div if a separate div is empty. Here\'s what I\'m using:

$(document).ready(function () {
    if (\'#leftmenu:empty\') {
             


        
10条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 03:46

    It depends what you mean by empty.

    To check if there is no text (this allows child elements that are empty themselves):

    if ($('#leftmenu').text() == '')
    

    To check if there are no child elements or text:

    if ($('#leftmenu').contents().length == 0)
    

    Or,

    if ($('#leftmenu').html() == '')
    

提交回复
热议问题