我在 jQuery 中的遍历有点困难。这是相关标记:
<ul id="sortable" class="ui-sortable">
<li>
<img src="http://ecx.images-amazon.com/images/I/51Vza76tCxL._SL160_.jpg">
<div class="controls">
<span class="move">✜</span>
<span class="delete">✘</span>
</div>
<div class="data">
<h1>War and Peace (Oxford World's Classics)</h1>
<textarea>Published to coincide with the centenary of Tolstoy's death, here is an exciting new edition of one of the great literary works of world literature.</textarea>
</div>
</li>
<li>
<img src="http://ecx.images-amazon.com/images/I/51boZxm2seL._SL160_.jpg">
<div class="controls">
<span class="move">✜</span>
<span class="delete">✘</span>
</div>
<div class="data">
<h1>A Christmas Carol and Other Christmas Writings (Penguin Classics)</h1>
<span>Optionally, write your own description in the box below.</span>
<textarea>Dicken's Christmas writings-in a new, sumptuous, and delightful clothbound edition.</textarea>
</div>
</li>
</ul>
这是 jQuery UI“可排序”元素的代码。这就是我想要发生的事情。
当单击“删除”( $('.delete')
) 时,我想要 <li>
其包含的项目将被删除。
我尝试过使用$('.delete').parent().parent().remove();
但在有两个项目的情况下,这似乎会删除它们。我对此有点困惑。我还尝试使用 closest()
找到最近的li
,但这似乎也不起作用。
在这种情况下我应该如何最好地遍历 DOM?
谢谢!
jack
最佳答案
尝试:
$('.delete').click(function(){
$(this).parentsUntil('li').parent().remove();
});
http://api.jquery.com/parentsUntil/
注意:在 jQuery 1.4+ 中可用
关于jquery - jQuery 遍历帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4685644/