Using jQuery to see if a div has a child with a certain class

后端 未结 5 863
时光取名叫无心
时光取名叫无心 2020-12-12 15:58

I have a div #popup that is dynamically filled with several paragraphs with the class .filled-text. I\'m trying to get jQuery to tell me if #

相关标签:
5条回答
  • 2020-12-12 16:23

    Use the children funcion of jQuery.

    $("#text-field").keydown(function(event) {
        if($('#popup').children('p.filled-text').length > 0) {
            console.log("Found");
         }
    });
    

    $.children('').length will return the count of child elements which match the selector.

    0 讨论(0)
  • 2020-12-12 16:30

    There is a hasClass function

    if($('#popup p').hasClass('filled-text'))
    
    0 讨论(0)
  • 2020-12-12 16:35

    Simple Way

    if ($('#text-field > p.filled-text').length != 0)
    
    0 讨论(0)
  • 2020-12-12 16:42

    If it's a direct child you can do as below if it could be nested deeper remove the >

    $("#text-field").keydown(function(event) {
        if($('#popup>p.filled-text').length !== 0) {
            console.log("Found");
         }
    });
    
    0 讨论(0)
  • 2020-12-12 16:50

    You can use the find function:

    if($('#popup').find('p.filled-text').length !== 0)
       // Do Stuff
    
    0 讨论(0)
提交回复
热议问题