jQuery-检查单击了哪个元素

时间:2020-06-20 14:00:28

标签: javascript html jquery

我创建了一个执行特定功能的函数。但是我不想一遍又一遍地重复该功能,只是在我的classes函数上更改了click。因此,我认为,如果将所有这些classes添加到函数中,代码看起来会更加简洁。但是问题是:如何检测我单击的元素。在这种情况下,假设我单击了.firstRowLeftTable。如何只向我的HTML类添加一些.first

$(".firstRowLeftTable .addFurcation, .secondRowLeftTable .addFurcation").click(function() {
    $(".first>div:nth-child("+($(this).index()+1)+")").html(...); // If "firstRowLeftTable" was clicked, add some HTML only to this element
    $(".second>div:nth-child("+($(this).index()+1)+")").html(...);
 });

1 个答案:

答案 0 :(得分:0)

使用点击事件属性target

#(".first>div").on("click", foo);

var foo = (e) => {
  var elem = e.target;
}
相关问题