如何使用jquery检查元素是否包含任何子元素?

时间:2021-07-28 08:41:18

I have a div that is floating left and the other floating right. I want to check if the div that is floating right has children element; if the it don't have any visible element, I want applied and new class to the left div. See below:

我有一个向左浮动的div,另一个向右浮动。我想检查右移的div是否有子元素;如果它没有任何可见元素,我想要应用左侧div的新类。见下文:

<div id="leftContent" class="left ">
    <table></table>
</div> 


<div id="rightContent" class="content">
    //the dom has no visible element
    //”#ctl00_ContentPlaceHolder1_ somegridView” is not visible     
</div> 

And I’m using the following script:

我正在使用以下脚本:

$(document).ready(function() {
    if ($(“#ctl00_ContentPlaceHolder1_ somegridView”).lenght = 0) {

        $("# leftContent ").removeClass("left");
        $("# leftContent ").addClass("center");


    }
});

div.left
{
    float: left;
    width: 365px;
    margin-left: 5px;
    padding-left: 2px;
}
div.center
{
    padding: 2px;
    margin: 5px;
    float: none;
    width: 95%;
    clear: both;
}

If div id="rightContent" empty?

如果div id =“rightContent”为空?

3 个解决方案

#1


13  

if ( $("#rightContent").children().length > 0)
{

   // do style changes

}

#2


11  

You can use is along with :empty.

你可以使用is with:empty。

if($('#rightContent').is(':empty')) {

}

#3


1  

Try this:

尝试这个:

if ($('#rightContent').children().length === 0) {
    //Whatever
}

EDIT: Correct ID

编辑:正确的ID

#1


13  

if ( $("#rightContent").children().length > 0)
{

   // do style changes

}

#2


11  

You can use is along with :empty.

你可以使用is with:empty。

if($('#rightContent').is(':empty')) {

}

#3


1  

Try this:

尝试这个:

if ($('#rightContent').children().length === 0) {
    //Whatever
}

EDIT: Correct ID

编辑:正确的ID