代码之家  ›  专栏  ›  技术社区  ›  RGriffiths

jquery检测被触摸的子菜单项的id

  •  0
  • RGriffiths  · 技术社区  · 6 年前

    我有一个下拉菜单,在非触摸设备上运行良好,但在ipad上查看时,例如,菜单不会在做出/触摸选择时消失。

    我可以在被触摸的菜单项上触发事件:

        $(document).on('touchstart', function() {
             ....
        });
    

    但我不知道如何确定是哪个ID触发了事件。

    我试过:

            var selectedID = $(this).attr("id");
    

    但这会导致未定义。菜单结构如下:

    <div class="dropdown">
        <button class="button_main" id="admin">Admin</button>
        <div class="dropdown-content">
            <button class="button_sub" id="admin_users">Users</button>
            <button class="button_sub" id="admin_depts">Departments</button>
        </div>
    </div> 
    

    我真正需要的是触摸子菜单项时主菜单id的id。有什么线索吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Louys Patrice Bessette    6 年前

    尝试:

    $(document).on('touchstart', "button", function(event) {
         var selectedID = $(event.target).attr("id");
         console.log(selectedID);
    });
    

    所以…在按钮上的Touchstart上…其他地方没有。然后使用 event 得到那个 target 属性,这是触发它的元素。