下拉菜单单击 eventListener 根据单击的区域生成不同的目标元素

安德鲁·泰勒

我有一个用这个 HTML 实现的 MaterializeCSS 下拉菜单。事件侦听器仅在单击上部的下拉项时才起作用。


    <div class="left">
      <span id="notificationTotal" class="new badge red" style="display:none"> 
      </span>
        <a class="dropdown-trigger" href="#!" data-target="notificationsDropdown">
          <i class="material-icons"> message</i>
        </a>
      </div>
      <ul id="notificationsDropdown" class="dropdown-content">
    </ul>

我正在使用以下 Javascript 来填充带有通知的菜单。这工作得很好。

    // Reset Notification Dropdown
    notificationsDropdown.innerHTML = '';

    notifications.forEach(notif => {
      const displayTime = moment(notif.date).fromNow();
      let typeIcon = 'sms';
      if (notif.type === 'chat') {
        typeIcon = 'lock';
      }

      notificationsDropdown.innerHTML += `<li class="notification">
      <a style="margin-top:0px;margin-bottom:0px;padding-bottom: 0;font-size:14px" href="#" class="blue-text">
      <span class="js-patientName">
      ${notif.contact.firstName} ${notif.contact.lastName}</span>
      <span class="js-notificationPhone" style="display:none">${
        notif.contact.phone
      }</span>
      <span class="js-patientId" style="display:none">${
        notif.patientId
      }</span>
      <span class="js-patientDOB" style="display:none">${
        notif.contact.birthDate
      }</span>
      <p style="margin-top:0px;margin-bottom:0px;padding-bottom: 0;padding-top: 0;">
        <i style="display: inline-flex; vertical-align:middle" class="tiny material-icons">${typeIcon}</i>
        <span class="black-text" style="font-size:12px">
        ${displayTime}
        </span>
      </p>
      </a></li>`;
    });
    notificationsDropdown.innerHTML += `<li class="divider" class="blue-text"></li><li><a href="/notifications" class="blue-text">See All Notifications</a></li>`;
  }

下拉列表被填充,当用户根据他们单击的确切位置单击特定的下拉列表 .li 条目时,它可能会也可能不起作用。用户必须单击下拉项的主要顶部。

这是从隐藏的 span 元素中提取值的事件侦听器代码。

  document
    .querySelectorAll('#notificationsDropdown', '.li .a .notification')
    .forEach(input =>
      input.addEventListener('click', async e => {
        // console.log('clicked', e.target);

        console.log(e.target.parentNode);

        const name = e.target.children[0].textContent.trim();
        const phone = e.target.children[1].textContent.trim();
        const patientId = e.target.children[2].textContent.trim();
        const birthDate = e.target.children[3].textContent.trim();
        console.log('patientid ', patientId);

        const patient = {
          name,
          patientId,
          phone,
          birthDate
        };

有没有办法重写 eventListener 代码来解决这个问题?可能不是使用 e.target.children[insert_number_here].textContent 我可以使用 .closest('js-patientId') 或类似的?

这就是 HTML 呈现到页面的方式。这是单个通知的示例:

<ul
  id="notificationsDropdown"
  class="dropdown-content"
  tabindex="0"
  style="display: block; width: 177.297px; left: 1648.7px; top: 0px; height: 251px; transform-origin: 100% 0px; opacity: 1; transform: scaleX(1) scaleY(1);"
>
  <li class="notification">
    <a
      style="margin-top:0px;margin-bottom:0px;padding-bottom: 0;font-size:14px"
      href="#"
      class="blue-text"
    >
      <span class="js-patientName">ANDREW TAYLOR</span>
      <span class="js-notificationPhone" style="display:none">
        5555551212
      </span>
      <span class="js-patientId" style="display:none">
        1
      </span>
      <span class="js-patientDOB" style="display:none">
        1960-01-01
      </span>
      <p style="margin-top:0px;margin-bottom:0px;padding-bottom: 0;padding-top: 0;">
        <i
          style="display: inline-flex; vertical-align:middle"
          class="tiny material-icons"
        >
          sms
        </i>
        <span class="black-text" style="font-size:12px">
          2 hours ago
        </span>
      </p>
    </a>
  </li>
  <li class="divider" />
  <li>
    <a href="/notifications" class="blue-text">
      See All Notifications
    </a>
  </li>
</ul>;
安德鲁·泰勒

我能够删除每个 li 中的 。然后调整值分配以使用以下内容:

e.target.parentNode.children[0].textContent.trim();

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Firefox下拉菜单单击

来自分类Dev

如何防止引导程序下拉菜单单击关闭父级下拉菜单

来自分类Dev

下拉菜单EventListener

来自分类Dev

纯CSS下拉菜单单击以隐藏还是显示div?

来自分类Dev

纯CSS下拉菜单单击以隐藏还是显示div?

来自分类Dev

单击下拉菜单

来自分类Dev

下拉菜单单击外部子菜单隐藏不适用于我的菜单类型(请先阅读)

来自分类Dev

单击下拉菜单滚动

来自分类Dev

单击jQuery或JavaScript菜单下拉菜单

来自分类Dev

单击后关闭下拉菜单

来自分类Dev

jQuery-单击下拉菜单

来自分类Dev

单击下拉菜单不会转到链接

来自分类Dev

VBA单击悬停下拉菜单

来自分类Dev

无法使用Selenium单击下拉菜单

来自分类Dev

单击CSS创建下拉菜单

来自分类Dev

单击按钮重新填充下拉菜单

来自分类Dev

单击后保留下拉菜单

来自分类Dev

单击下拉菜单不会转到链接

来自分类Dev

通过单击按钮影响下拉菜单

来自分类Dev

单击变量ID的下拉菜单

来自分类Dev

Bootstrap下拉菜单链接不可单击

来自分类Dev

单击以查看下拉菜单

来自分类Dev

下拉菜单仅通过单击按钮

来自分类Dev

下拉菜单仅单击选定的行

来自分类Dev

单击时关闭下拉菜单

来自分类Dev

单击两个下拉菜单

来自分类Dev

使下拉菜单停留直到单击按钮

来自分类Dev

根据类别更改下拉菜单以匹配单击的链接

来自分类Dev

HTML标题菜单下拉菜单无法单击