这听起来像一些后代元素上有边框或填充,所以 event.target 是(有效地)连接处理程序的元素的后代。
event.target
两种选择:
使用 this.id 得到的 id 处理程序(有效)绑定到的元素。这通常会做你想要的。 更新小提琴
this.id
id
在这种情况下我认为你不需要它,但工具包中另一个方便的工具是 closest ,通过查看您给出的元素,然后查看其父元素,找到匹配选择器的第一个元素 它的 父母等等,例如, $(this).closest(".item").attr("id") 要么 $(event.target).closest(".item").attr("id") (因为你想要的项目有课程 item )。 更新小提琴 但同样,我相信在这种情况下你想要的是#1。
closest
$(this).closest(".item").attr("id")
$(event.target).closest(".item").attr("id")
item