Using an if statement to check if a div is empty

前端 未结 10 1132
忘了有多久
忘了有多久 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条回答
  •  猫巷女王i
    2020-11-28 03:20

    You can use .is().

    if( $('#leftmenu').is(':empty') ) {
        // ...
    

    Or you could just test the length property to see if one was found.

    if( $('#leftmenu:empty').length ) {
        // ...
    

    Keep in mind that empty means no white space either. If there's a chance that there will be white space, then you can use $.trim() and check for the length of the content.

    if( !$.trim( $('#leftmenu').html() ).length ) {
        // ...
    

提交回复
热议问题