简体   繁体   English

用jQuery看一个div有没有某某的子class

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

I have a div #popup that is dynamically filled with several paragraphs with the class .filled-text .我有一个 div #popup ,它用 class .filled-text动态填充了几个段落。 I'm trying to get jQuery to tell me if #popup has one of these paragraphs in it.我试图让 jQuery 告诉我#popup中是否包含这些段落之一。

I have this code:我有这个代码:

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

Any suggestions?有什么建议么?

You can use the find function: 您可以使用find功能:

if($('#popup').find('p.filled-text').length !== 0)
   // Do Stuff

有一个hasClass函数

if($('#popup p').hasClass('filled-text'))

Use the children funcion of jQuery. 使用儿童的jQuery funcion。

$("#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. $.children('').length将返回与选择器匹配的子元素的数量。

简单的方法

if ($('#text-field > p.filled-text').length != 0)

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");
     }
});

if you have multiple divs with same class and some only have that class you must check each one:如果您有多个具有相同类的 div 并且有些只有该类,则必须检查每个:

            $('.popup').each(function() {
                if ($(this).find('p.filled-text').length !== 0) {
                    $(this).addClass('this-popup-has-filled-text');
                }
            });

In the 10 years since you've asked this question, jQuery has added almost exactly the .has( function that you described above. It filters the selector it's called on -- and it's faster than using $('.child').parent('.parent') and potentially running all the way up the DOM.自您提出这个问题以来的 10 年中,jQuery 几乎完全添加了您上面描述的.has( function 。它过滤了它所调用的选择器 - 它比使用$('.child').parent('.parent')更快。 $('.child').parent('.parent')并可能一直运行到 DOM。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查以查看子div在父DIV中具有特定链接 - check to see a child div has certain link in a parent DIV 如果任何子元素具有某个类,则将类添加到父div,否则将其删除(jQuery或Javascript) - Add class to parent div if any of the child elements has a certain class, remove if not (jQuery or Javascript) 使用选择器的jquery的if else语句,以查看是否已将某个div附加到该语句 - If else statement for jquery using selectors to see if a certain div has been appended to it 当child具有某个类时,jQuery toggleClass为父类 - jQuery toggleClass to parent class when child has a certain class jQuery 如果 div 在某些 div 中有某些文本,则隐藏具有某些类的第一个 div - jQuery if div has certain text in certain div, hide FIRST div with certain class 如果孩子使用jQuery,则将类应用于父类 - applying class to parent if child has it using jQuery 如何检查具有href =“ something”的div是否具有Jquery的某些类 - How to check if a div with href=“something” has certain class with Jquery 如何将类添加到 div 中,其中无线电对 jquery 具有一定的价值 - how to add class to div in which radio has certain value with jquery 如何使用 puppeteer 检查 div 是否具有某个 class? - How can I check if a div has a certain class using puppeteer? 使用 jQuery 将 class 添加到具有相同 class 的 div 的子元素 - Add class to child elements of div's with same class using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM