JQuery:在单击父级的光标下获取子级

时间:2012-03-30 10:42:34

标签: jquery click cursor parent-child

假设我们有一个这样的基本列表:

<ul id="list">
  <li></li>
  <li></li>
  <li></li>
</ul>

$('#list').on('click', function() {
  // how to get the li element that was clicked within the ul element?
});

2 个答案:

答案 0 :(得分:3)

这很简单:

$('#list').on('click','li', function() {
  // how to get the li element that was clicked within the ul element?
});

通过将选择器作为额外参数添加到 .on()方法,您可以访问触发事件的元素。

答案 1 :(得分:0)

不确定为什么您不想直接将点击事件分配给li,但我认为您希望稍后在代码中使用ul执行某些操作,那么如何执行其他操作呢?四处走动?

$('#list li').on('click', function() {
   // if you need to get parent, do
   var parent = $(this).parent();
});
相关问题