Jquery 如何选择子元素?

Jquery 如何选择子元素?,jquery,Jquery,html 我知道.find函数,但想知道另一种方法。试试下面的一行: 希望它能对您有所帮助。您也可以这样做: $('.one').children().children('.three').css('color','#f00'); $('.one').children('.two').children('.three').css('color','#f00'); $('.one').children('.two').find('.three').css('color','#f00'); 或者当

html

我知道.find函数,但想知道另一种方法。

试试下面的一行:

希望它能对您有所帮助。

您也可以这样做:

$('.one').children().children('.three').css('color','#f00');
$('.one').children('.two').children('.three').css('color','#f00');
$('.one').children('.two').find('.three').css('color','#f00');
或者当然:

$('.one > .two').children('.three').css('color','#f00');

这里有一个提示:

您可以随时更改选择器以将类链接在一起:

$('.one > .two > .three').css('color','#f00');

这将选择.one类中包含该类的所有子类。三个

您可以使用上下文选择器:

$(".one .three").css("color", "#f00");

以下方法可能对您有所帮助:

$('.three', $('#one')).css(.....
试一试

试试这个


这完全取决于你到底想要实现什么

如果类为3的项目在您的文档中只存在一次,或者希望获得所有这些项目,则可以使用$'.3'

如果要查找任何三个是一个或两个是一个的后代,请使用$'.one.two.three'

如果您只想在他们是直接子对象的情况下使用$'.one>.two>.three'

如果碰巧某个节点已经有一个jquery对象,则可以限制以前的搜索,如下所示: $'.three',theOne,$'.two.three',theOne或$'>.two>.three',theOne


我希望其中一个能满足您的需求,如果不能满足,那么您最好澄清具体情况。

请先阅读问题posting@musefan足够公平:编辑:在jQuery中有很多方法可以选择子元素。我建议浏览网页可以更好地利用你和其他人的时间。find有什么问题吗?为什么需要其他方法?正确的方法是使用find。时期子级遍历树中的1个级别,查找遍历所有级别。例如,如果op在查找方面有个人问题,可以使用上下文选择器。jQuery选择器[,上下文]。这也会在内部转化为查找。这是错误的,不应该在那里最后一个是我要找的最后一个是$'>.two>.three',theOne?$'.three',theOne你能给我看一个演示我的html吗?这会找到任何三个节点是theOne的decentant。这是一个小提琴手,显示它在工作:
$(".one .three").css("color", "#f00");
$('.three', $('#one')).css(.....
var children=$('.one').children();

children.each(function(idx, val){
   $(this).css('color','#f00');
});
$('.one > div.three').css('color','#f00');
$("div").children(".two").css("color", "blue");