我更喜欢使用 eq ( http://api.jquery.com/eq/ ) 过滤器从单击的目标元素中过滤第三个父项。看看这个片段!
<html>
<head>
<script type="text/javascript">
$(".trigger").bind("click", function(event) {
var el = $(event.currentTarget);
var parent = el.parents().eq(3);
parent.toggleClass("active", true);
});
</script>
</head>
<body>
<div class="accordion">
<div class="head">
<h1>
<span class="httpmethod">
<a class="trigger">Link 1</a>
</span>
</h1>
</div>
</div>
<div class="accordion">
<div class="head">
<h1>
<span class="httpmethod">
<a class="trigger">Link 2</a>
</span>
</h1>
</div>
</div>
<div class="accordion">
<div class="head">
<h1>
<span class="httpmethod">
<a class="trigger">Link 3</a>
</span>
</h1>
</div>
</div>
</body>