简体   繁体   English

JavaScript onclick() 事件冒泡 - 工作与否?

[英]JavaScript onclick() event bubbling - working or not?

I have a table in which the table row tag is decorated with an onclick() handler.我有一个表,其中表行标记装饰有onclick()处理程序。 If the row is clicked anywhere, it will load another page.如果在任何地方单击该行,它将加载另一个页面。 In one of the elements on the row, is an anchor tag which also leads to another page.在该行的一个元素中,是一个锚标记,它也指向另一个页面。

The desired behavior is that if they click on the link, "delete.html" is loaded.期望的行为是,如果他们单击链接,将加载“delete.html”。 If they click anywhere else in the row, "edit.html" is loaded.如果他们单击该行中的任何其他位置,则会加载“edit.html”。

The problem is that sometimes (according to users) both the link and the onclick() are fired at once, leading to a problem in the back end code.问题是有时(根据用户)链接和onclick()都会同时触发,从而导致后端代码出现问题。 They swear they are not double-clicking.他们发誓他们没有双击。

I don't know enough about JavaScript event bubbling, handling and whatever to even know where to start with this bizarre problem, so I'm asking for help.我对 JavaScript 事件的冒泡、处理以及其他任何事情都知之甚少,甚至不知道从哪里开始解决这个奇怪的问题,所以我正在寻求帮助。 Here's a fragment of the rendered page, showing the row with the embedded link and associated script tag.这是呈现页面的片段,显示带有嵌入式链接和关联脚本标记的行。 Any suggestions are welcomed:欢迎任何建议:

<tr id="tableRow_3339_0" class="odd">
   <td class="l"></td>
   <td>PENDING</td>
   <td>Yabba Dabba Doo</td>
   <td>Fred Flintstone</td>
   <td>
     <a href="/delete.html?requestId=3339">
       <div class="deleteButtonIcon"></div>
     </a>
  </td>
  <td class="r"></td>
</tr>
<script type="text/javascript">document.getElementById("tableRow_3339_0").onclick = function(event) { window.location = '//edit.html?requestId=3339'; };</script>

Event bubbling work like this. 事件冒泡是这样的。 If an element A is contained within element B, and element A is clicked, the click event fires for element A and then it will bubble up and fire for element B. This occurs because technically you are clicking both elements. 如果元素A包含在元素B中,并且单击了元素A,则对元素A触发click事件,然后将其冒泡并为元素B触发。这是因为从技术上讲,您同时单击两个元素。

So basically if they click the link, the click event bubbles up to the tr element. 因此,基本上,如果他们单击链接,单击事件就会冒泡到tr元素。 Depending on how fast your next page loads from the link click, the tr click event may occur. 取决于您的下一页从链接单击加载的速度,可能会发生tr click事件。

You can stop bubbling within an event handler: How to stop event propagation with inline onclick attribute? 您可以在事件处理程序中停止冒泡: 如何使用内联onclick属性停止事件传播?

There's 3 primary ways to handle JavaScript events: 处理JavaScript事件的主要方式有3种:

  1. Event Handlers 事件处理程序
  2. Event Listeners 事件监听器
  3. Event Delegation 活动委托

Event handlers are things like onclick that are put directly into the HTML. 事件处理程序是诸如onclick类的东西,它们直接放入HTML中。 Event listeners use a method called addEventListener to attach listners to an element and has the browser listen for events. 事件侦听器使用称为addEventListener的方法将列表器附加到元素,并使浏览器侦听事件。 Event delegation (involves bubbling), per your question, and allows us to listen on parent elements to events that happen on their children. 根据您的问题进行事件委派(涉及冒泡),并允许我们侦听其父级元素上发生在其子级上的事件。

You're basically trying to blend #1 and #3 here. 您基本上是在尝试将#1和#3融合在一起。 You could use any of the three but you might actually have an easier time with #2: 您可以使用这三个中的任何一个,但实际上使用#2可能会更轻松:

Try something like this: 尝试这样的事情:

rows = document.getElementsByTagName("tr");
rows.('click', function(event) {
 window.location = '//edit.html?requestId=3339'; 
};

This post on JavaScript Events will cover more on this if you're needing to understand the 3 ways better. 如果您需要更好地了解这三种方式,则有关JavaScript事件的这篇文章将涵盖更多内容。 (Disclaimer: I wrote it). (免责声明:我写的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM