jQuery遍历div以上

nam*_*tax 1 jquery traversal

有一些JQuery遍历和寻找一些辅助的问题..

如果我有以下HTML

<div id="1">
This is a div
</div>
<div id="2">
<a href="link">This is div 2</a>
</div>
Run Code Online (Sandbox Code Playgroud)

我想做的是,当我点击div 2中的链接时,是使用dom遍历向div1添加一个类,而不仅仅是直接引用div 1s id ....

非常感谢

nor*_*ron 5

http://api.jquery.com/prev/

这应该这样做:

$('a').click(function() {
  $(this).parent().prev().addClass('previous');
});
Run Code Online (Sandbox Code Playgroud)