如何选择所有没有特定类的父级的输入框?
在这里,我只想获取没有带有 not-parent
类的父级的输入框。
$(document).ready(function(){
$('input:text').css('color', '#FF1200');
});
我尝试过使用filter()
,但没有成功。
谢谢!
如何选择所有没有特定类的父级的输入框?
在这里,我只想获取没有带有 not-parent
类的父级的输入框。
$(document).ready(function(){
$('input:text').css('color', '#FF1200');
});
我尝试过使用filter()
,但没有成功。
谢谢!
如果您的输入都包含在div中;
如果您想要从顶级到底级进行父级检查:
$("div:not(.not-parent) input").css('color', '#FF1200');
或者,如果您只想要一个层级的父级检查:
$("div:not(.not-parent) > input").css('color', '#FF1200');
编辑:如果它的祖先是同一类型但没有class,请使用以下代码:
$('input').filter(function(){
return !$(this).parents('div').hasClass('not-parent');
}).css('color', '#FF1200');
$("div.parent input").css('color', '#FF1200');
是最简单的解决方案... - Mark Pieszak - Trilon.ioinput[type="text"]
,因为它使用CSS选择器(并且速度更快)而不是已弃用的:text
。 - Mark Pieszak - Trilon.io