Using an if statement to check if a div is empty

前端 未结 10 1130
忘了有多久
忘了有多久 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:45

    I've encountered this today and the accepted answers did not work for me. Here is how I did it.

    if( $('#div-id *').length === 0 ) {
        // your code here...
    }
    

    My solution checks if there are any elements inside the div so it would still mark the div empty if there is only text inside it.

提交回复
热议问题