Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery获取子对象的子对象的子对象_Jquery - Fatal编程技术网

jquery获取子对象的子对象的子对象

jquery获取子对象的子对象的子对象,jquery,Jquery,好吧,这可能有点基本。HTML是: <div class="parents-of-all"> <p> <a> <span class="thechild">Something</span> </a> </p> 但不知怎么的,它不起作用。在这里测试它: 我的问题是我如何穿越到跨度?我通常的方式是 $(this).children().children().children().a

好吧,这可能有点基本。HTML是:

<div class="parents-of-all">
<p>
    <a>
        <span class="thechild">Something</span>
    </a>
</p>
但不知怎么的,它不起作用。在这里测试它:

我的问题是我如何穿越到跨度?我通常的方式是

$(this).children().children().children().attr('class');
我确信还有比这更短的方法,使用find()就是其中之一,但我似乎无法让它工作

非常感谢

编辑:嗯!显然,我忘了带钥匙。用于所有DOM选择器的父级。有时候最简单的错误就在你的脸上


但是,使用find()和multiple children()有什么区别吗?我发现使用multiple children()可以确保更精确的遍历,因为我们可以根据需要添加元素选择器,但还有其他主要区别吗?

您的类选择器中缺少点:

$('.parents-of-all').click(function(){
    alert($(this).find('span').attr('class'));
});

如果要更改具有祖先的子级或具有父级的子级的属性,应使用相应的选择器:

$('.parents-of-all').click(function(){
    alert($(this).find('span').attr('class'));
});
例如:

$('.parents-of-all span').addClass('hello');
将类“hello”应用于所有
元素,这些元素是类
所有
的父元素的后代