简体   繁体   English

Javascript点击事件触发两次

[英]Javascript click event triggers twice

In the following snippet, why does divClicked() trigger twice when the <label> is clicked, but only once when <input> is clicked?在以下代码段中,为什么divClicked()在单击<label>时触发两次,而在单击<input>时仅触发一次

 function divClicked(_index) { console.log("div click event"); } function inputClicked(_index) { console.log("input click event"); }
 <div class="option" onclick="divClicked(1)"> <input id="1_1" name="group_1" type="radio" value="1" onclick="inputClicked(1)" /> <label for="1_1">label</label> </div>

Note: I want to know why this happens, not a "quick fix" like: put onclick() on label .注意:我想知道为什么会发生这种情况,而不是“快速修复”,例如:将 onclick() 放在 label 上

This happens because of what the HTML spec describes at 4.10.4 :这是因为 HTML 规范在4.10.4 中的描述:

For example, on platforms where clicking a checkbox label checks the checkbox, clicking the label in the following snippet could trigger the user agent to run synthetic click activation steps on the input element, as if the element itself had been triggered by the user:例如,在单击复选框标签检查复选框的平台上,单击以下代码段中的label可能会触发用户代理在input元素上运行合成点击激活步骤,就好像元素本身已被用户触发一样:

 <label><input type=checkbox name=lost> Lost</label>

On other platforms, the behavior might be just to focus the control, or do nothing.在其他平台上,行为可能只是为了集中控制,或者什么都不做。

This means that when a <label> is clicked, the browser creates a second "synthetic" click event on the associated <input> element, in order to toggle its state.这意味着当点击<label> ,浏览器会在关联的<input>元素上创建第二个“合成”点击事件,以切换其状态。

The reason divClicked is triggered twice, is because the first event which comes from the <label> bubbles up to the <div> , and also the second, synthetic click event bubbles up to the <div> . divClicked被触发两次的原因是因为来自<label>的第一个事件冒泡到<div> ,第二个合成点击事件冒泡到<div>

This is usually be cause of the bubbling principle of click event:这通常是click事件bubbling原理的原因:

When an event happens on an element, it runs on it, its associated elements,its parent and other ancestors.当一个事件发生在一个元素上时,它会在它、它的关联元素、它的父元素和其他祖先元素上运行。

Now, The relation is when you click on label there a are two events which bubbles up here:现在,关系是当您单击label ,有两个事件会在此处冒泡:

1) Click on div (which you expect) 1)点击div(你期望的)

2) Click on input (which is also expected) 2)点击输入(这也是预期的)

2.1) When click on input is triggered then a click on div is also triggered again here 2.1) 当点击input被触发时,这里也会再次触发对div点击

You can confirm this behavior by using event.bubbles prop.您可以使用event.bubbles道具确认此行为。

EDIT:编辑:

The reason for the connection between label and input : (I know this is absolutely not required, as it's present all over the place yet) labelinput之间连接的原因:(我知道这绝对不是必需的,因为它无处不在)

A <label> can be associated with a control either by placing the control element inside the <label> element, or by using the for attribute. <label>可以通过将控件element inside the <label>元素内or by using the for属性与控件相关联。 Such a control is called the labeled control of the label element.这样的控件称为标签元素的标记控件。 One input can be associated with multiple labels.一个输入可以与多个标签相关联。

Taken from:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label摘自:https ://developer.mozilla.org/en-US/docs/Web/HTML/Element/label

Which means placing for on label referencing id of an input element will stimulate the behavior as if the element is inside the label .这意味着在标签上放置for引用输入元素的id将刺激行为,就好像元素在label This would bubble a event on input onto label like any event on child to parent这会将input上的事件bubblelabel就像childparent任何事件一样

在某些时候,还要检查 javascript 文件资产是否没有加载两次......它不应该发生,但你永远不知道。

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

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