简体   繁体   English

如何使用自定义HTMLElement作为目标触发更改事件?

[英]How to trigger a change event with a custom HTMLElement as the target?

I've an eventlistener registred on a section HTML tag which embedded various dropdowns: 我已经在一个事件侦听registred section ,其嵌入各种下拉菜单HTML标记:

function dropdownChange(e) {
    window.console.log("The event is detected");
    if (e.target.tagName === "SELECT")
        window.console.log("The event's target is a dropdown"); // Some jobs
    else
        window.console.log("The event's target is NOT a dropdown"); // Some jobs
}

document.getElementById("filterSection").addEventListener("change", dropdownChange);

It works fine. 它工作正常。

That I want to do is to trigger once a change event when my page is loaded. 我想要做的是在我的页面加载时触发一次更改事件。 As far as I understand the Mozilla documentation I should perform when my DOM is ready the following action: 据我了解Mozilla文档,我应该在我的DOM准备好后执行以下操作:

document.getElementById("periode").dispatchEvent(new Event("change"));

But the event is not trigger (I get no trace into my console). 但事件不是触发器(我无法进入我的控制台)。 I thought that the event propagation would make the job... 我认为事件传播会成功...

If I execute: 如果我执行:

document.getElementById("filterSection").dispatchEvent(new Event("change"));

The event is well triggered but the target is not the good one. 该事件被很好地触发,但目标不是好事。

What am I doing wrong? 我究竟做错了什么?

Dropdowns' HTML code: 下拉列表的HTML代码:

<section id="filterSection">
    <label>Période</label>
    <select name="periode" id="periode">
        <option value="2014-08">2014 Août</option>
        <option value="2014-07">2014 Juillet</option>
        <option value="2014-06">2014 Juin</option>
        <option value="2014-05">2014 Mai</option>
        <option value="2014-04">2014 Avril</option>
        <option value="2014-03">2014 Mars</option>
        <option value="2014-02">2014 Février</option>
        <option value="2014-01">2014 Janvier</option>
        <option value="2013-12">2013 Décembre</option>
        <option value="2013-11">2013 Novembre</option>
        <option value="2013-10">2013 Octobre</option>
        <option value="2013-09">2013 Septembre</option>
        <option value="2013-08">2013 Août</option>
        <option value="2013-07">2013 Juillet</option>
        <option value="2013-06">2013 Juin</option>
    </select>
    <!-- Other dropdowns...-->
</section>

My main browser target is Firefox.17 我的主要浏览器目标是Firefox.17

I would suggest using document.querySelector instead of getById, since it is more reliable. 我建议使用document.querySelector而不是getById,因为它更可靠。

So change adding/dispatching events to this below. 因此,请在下面更改添加/调度事件。

document.querySelector("#periode").addEventListener("change", dropdownChange);

document.querySelector("#periode").dispatchEvent(new Event("change"));

UPDATE: To have a parent, and let all children have the same event , following code: 更新:要拥有父级,并让所有孩子拥有相同的事件,请遵循以下代码:

function dropdownChange(e) {
    window.console.log("The event is detected");
    if (e.target.tagName === "SELECT") window.console.log("The event's target is a dropdown");
    else window.console.log("The event's target is NOT a dropdown");
}
//shiv, add a forEach to nodeList prototype to use forEach method on querySelectorAll lsit.
NodeList.prototype.forEach = Array.prototype.forEach; 

var parent = document.querySelectorAll("#filter select");
parent.forEach(function(e){
    e.addEventListener("change", dropdownChange);
});
document.querySelector("#filter select").dispatchEvent(new Event("change"));

http://jsfiddle.net/Holybreath/5sda7zsc/6/ http://jsfiddle.net/Holybreath/5sda7zsc/6/

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

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