防止父元素点击事件在点击子下拉菜单时触发

Prevent parent element click-event to trigger when child-dropdown clicked

document.getElementById("entirerow"+doc.id).addEventListener("click", function(e){
  alert("Entire row clicked")
})

document.getElementById("dropdown"+doc.id).addEventListener("click", function(e){
  alert("Only button clicked")
  //e.stopPropagation(); I have tried this but it prevents the dropdown from triggering at all.
})
<tr id="entirerow"+doc.id>
  <td>
  <span>
    <button id="dropdown"+doc.id>myDropdown</button>
  </span>
  </td>
  <td>
    ...
  </td>
</tr>

如果事件目标是下拉列表,您可以 return 在父侦听器回调的早期。

document.getElementById("entirerow"+doc.id).addEventListener("click", function(e){
  if(e.target.id === ("dropdown"+doc.id)){
    return;
  }

  alert("Entire row clicked");
})

document.getElementById("dropdown"+doc.id).addEventListener("click", function(e){
  alert("Only button clicked");
})