提问者:小点点

jQuery-检查单击了哪个元素


我创建了一个函数,做一个特定的工作。 但我不想一遍又一遍地重复那个函数,只是为了更改我的单击函数中的。 所以我想,如果我把所有这些都添加到我的函数中,代码看起来会干净得多。 但问题是:我怎样才能检测到我点击的是哪个元素。 在本例中,假设我单击了.firstRowleftTable。 如何只向.first类添加一些HTML

null

$(".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(...);
 });

null


共1个答案

匿名用户

使用单击事件属性target

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

var foo = (e) => {
  var elem = e.target;
}