与班级一起选择div,除非它有另一个班级的祖先 [英] select div with class unless it has ancestor of another class

查看:76
本文介绍了与班级一起选择div,除非它有另一个班级的祖先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

十分钟前提出了这个问题,但意识到我使用了父母"一词而不是祖先.我正在寻找某种涉及具有特定祖先的类的东西.

Asked this question ten minutes ago, but realized that I used the term parent instead of ancestor. I'm looking for something that refers to a class that has a particular ancestor.

如何选择没有特定类父级的所有输入?

How can I select all inputs that do not have an parent of a particular class?

在这里,我只想获取没有父类而不是父类的输入

Here I just want to get the inputs that do not have a parent with the class not-parent

$(document).ready(function() {

    $("div:not(.not-ancestor) > input[type='text']").css('color', '#FF1200');
});​

http://jsfiddle.net/MFtv3/3/

谢谢!

推荐答案

您可以使用过滤器,并通过使用closet()来检查元素是否在您要查找的类的上方有一个父元素,如下所示:

You can use a filter and check if the element has a parent somewhere above it with the class you're looking for by using closest(), like this :

$("div > input[type='text']").filter(function() {
    return !$(this).closest('.not-ancestor').length;
}).css('color', '#FF1200');

> FIDDLE

编辑

要获取元素集合以供进一步使用:

To get the collection of elements for further use:

var elems = $("div > input[type='text']").filter(function() {
                return !$(this).closest('.not-ancestor').length;
            });

elems.css('color', 'red');
elems.each(function(idx, elem) {
    $(elem).data('index', idx);
});

这篇关于与班级一起选择div,除非它有另一个班级的祖先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆