Javascript Jquery滑块->;如何正确遍历DOM

Javascript Jquery滑块->;如何正确遍历DOM,javascript,jquery,dom,Javascript,Jquery,Dom,我的代码: <div id = "1"> <h1>Heading 1</h1> <p class = "V">VVV</p> <ul> <li>line 1</li> <li>line 2</li> </ul> <p class = "A">AAA</p> </di

我的代码:

<div id = "1">
    <h1>Heading 1</h1>
    <p class = "V">VVV</p>
    <ul>
        <li>line 1</li>
        <li>line 2</li>
    </ul>
    <p class = "A">AAA</p>
</div>

<div id = "2">
    <h1>Heading 2</h1>
    <p class = "V">VVV</p>
    <ul>
        <li>line 1</li>
        <li>line 2</li>
    </ul>
    <p class = "A">AAA</p>
</div>


$('ul').hide();
$('p.A').hide();
$('p.V').click(function(){
        $(this).next('ul').slideDown('slow');
        $(this).hide();
        $(this).closest('p.A').show(); // <-- How do I select 'p.A' in the current div?    
    });

标题1

VVV

  • 第1行
  • 第2行

AAA

标题2

VVV

  • 第1行
  • 第2行

AAA

$('ul').hide(); $('p.A').hide(); $('p.V')。单击(函数(){ $(this.next('ul')。slideDown('slow'); $(this.hide(); $(this).closest('p.A').show();//该方法在DOM树上查找您可能想要使用的祖先

或和:
$(this.parent().find('p.A').show();

或者:
$(this).sibbines('p.A').show();

我可能会使用
$(this).parent().find('p.A')
,因为它对HTML的排列方式最不敏感。

替换

$(this).closest('p.A').show();

$(this).siblings('p.A:first').show();