[英]How to trigger event on target's sibling
I have the below mark up and scripts.我有以下标记和脚本。 I have a span with rootnode as id and it's sibling is input checkbox.我有一个以 rootnode 作为 id 的跨度,它的兄弟是输入复选框。
my requirement is when i click on the span, input check box onchanange handler should call with the input event object.我的要求是当我单击跨度时,输入复选框 onchanange 处理程序应使用输入事件对象调用。
pleas find the below code snippet as well as i have give codepen url with the example.请找到下面的代码片段,以及我在示例中给出了 codepen url。
HTML HTML
<div>
<span id="rootnode" role="checkbox" aria-checked="false">
<span>i am bike</span>
</span>
<input tabindex="-1" type="checkbox" onchange="handleChange()" name="vehicle1" value="Bike">
</div>
JAVASCRIPT爪哇脚本
document.getElementById("rootnode").addEventListener('click',this.handleclick);
function handleChange(e) {
alert(1)
}
function handleclick(e) {
alert('event fired..');
// how to call onchange handler handleclick with input event object.
}
Example URL : https://codepen.io/anon/pen/ejKqzy?editors=1010示例网址: https : //codepen.io/anon/pen/ejKqzy?editors=1010
Your help is highly appreciated.非常感谢您的帮助。
Thank You!!!谢谢你!!!
You can simply create an event in the handler and then dispatch it, like so:您可以简单地在处理程序中创建一个事件,然后分派它,如下所示:
document.getElementById("rootnode").addEventListener("click", this.handleclick); function handleChange(e) { alert(1); } function handleclick(e) { alert("event fired.."); if ("createEvent" in document) { let evt = document.createEvent("HTMLEvents"); evt.initEvent("change", false, true); document.querySelector("input[name=vehicle1]").dispatchEvent(evt); } else element.fireEvent("onchange"); }
<div> <span id="rootnode" role="checkbox" aria-checked="false"> <span>i am bike</span> </span> <input tabindex="-1" type="checkbox" onchange="handleChange()" name="vehicle1" value="Bike"> </div>
CodePen: https://codepen.io/lucakiebel/pen/yqEmXP代码笔: https ://codepen.io/lucakiebel/pen/yqEmXP
You should create and dispatch the event manually:您应该手动创建和调度事件:
var event = document.createEvent('Event');
event.initEvent("change", false, true);
var input = document.getElementsByName("vehicle1")[0];
input.dispatchEvent(event);
The documentation for creating an event is here and for initializing an event here .创建事件的文档在这里,初始化事件的文档在这里。
For further information you can visit a similar post (possible duplicate): How can I trigger an onchange event manually?有关更多信息,您可以访问类似的帖子(可能是重复的): 如何手动触发 onchange 事件?
Unless you're trying to do something something that isn't clear in your question/example, and if all you want is to have a label that trigger's the checkbox's event and have it be accessible, then you should use alabel element.除非您试图做一些在您的问题/示例中不清楚的事情,并且如果您只想拥有一个标签来触发复选框的事件并使其可访问,那么您应该使用标签元素。 There's two ways you can achieve this using just HTML and no JS:有两种方法可以仅使用 HTML 而不使用 JS 来实现此目的:
Set the label's for
attribute to the checkbox's id
:将标签的for
属性设置为复选框的id
:
function handleChange(opt) { alert(opt) }
<label for="vehicle1">i am bike</label> <input tabindex="-1" type="checkbox" onchange="handleChange(1)" id="vehicle1" name="vehicle1" value="Bike">
Wrap the checkbox as a child of the label:将复选框包装为标签的子项:
function handleChange(opt) { alert(opt) }
<label for="vehicle1">i am bike</label> <input tabindex="-1" type="checkbox" onchange="handleChange(1)" id="vehicle1" name="vehicle1" value="Bike">
Both solutions are far more standard and accessible.这两种解决方案都更加标准且易于访问。
https://codepen.io/jerred121/pen/dyXrGmB?editors=1010 https://codepen.io/jerred121/pen/dyXrGmB?editors=1010
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.