好的,这可能有点基础。HTML是:
<div class="parents-of-all">
<p>
<a>
<span class="thechild">Something</span>
</a>
</p>
Run Code Online (Sandbox Code Playgroud)
jQuery是
$('.parents-of-all').click(function(){
alert($(this).find('span').attr('class'));
});
Run Code Online (Sandbox Code Playgroud)
但是不知何故,它不起作用。在这里测试:
我的问题是如何遍历跨度?我通常的方法是
$(this).children().children().children().attr('class');
Run Code Online (Sandbox Code Playgroud)
我敢肯定有比这更短的方法,并且使用find()是其中一种,但是我似乎无法使其工作。
非常感谢!
编辑:!显然我忘记了。用于所有父级DOM选择器。有时最简单的错误就在您的脸上。
但是,再次使用find()和多个children()有什么区别吗?我发现使用多个children()可以确保更准确的遍历,因为我们可以根据需要添加元素选择器,但是还有其他主要区别吗?
您在类选择器中缺少点:
$('.parents-of-all').click(function(){
alert($(this).find('span').attr('class'));
});
Run Code Online (Sandbox Code Playgroud)
http://jsfiddle.net/BoltClock/3zn7e/2
| 归档时间: |
|
| 查看次数: |
16421 次 |
| 最近记录: |