即使在类名上触发了click事件,也要对特定元素执行某些操作

Do something with the particular element even though click event is fired on class name

本文关键字:元素 操作 执行 事件 click      更新时间:2023-09-26

我有一个场景,下拉启动程序菜单应该出现在列表页面的每一行上。我已经调整了这里的代码。

我希望在点击特定启动器图标时限制popover(打开)行为,尽管关闭操作在这里是完美的。

问题是,当点击任何图标时,它都会显示所有菜单。我的页面有从数据库中膨胀的行,每行都有三个这样的启动器图标。

我想这段代码需要一些调整:

// Click event handler to toggle dropdown
$(".button").click(function(event){
    event.stopPropagation();
    // How can I call toggle for the specific div element?
    $(".app-launcher").toggle();
});

如何做到这一点?

您需要遍历DOM以找到与单击的.button元素相关的.app-launcher实例,为此,请使用closest()find(),如下所示:

$(".button").click(function(event){
    event.stopPropagation();
    $(this).closest('.launcher').find(".app-launcher").toggle();
});

更新的CodePen

我还建议考虑使用.app-launcher的单个实例,并根据需要在DOM中移动它,以清空HTML代码。