简体   繁体   中英

how to trigger childNode click event

I have a parent DIV and it has many child divs. Those child divs are clickable, and I am trying to trigger click event on each divs in order with setInterval(funcLoop, 5000)

....
setInterval(funcLoop, 5000);
....
....
function funcLoop()
{
    var c = document.getElementById("divParent").childNodes;
    c[index].click();
    index++;
    if (index == document.getElementById("divParent").childNodes.length)
        index = 0;
}

But, I am getting the error below

Uncaught TypeError: c[index].click is not a function

can anybody tell me what I should do to fix this?

childNodes also includes text nodes and comments, which does not have click handlers.

Replace childNodes with children , or do

setInterval(funcLoop, 5000);
....
....

function funcLoop() {
    var c = document.getElementById("divParent").children;

    for (var i=c.length; i--;) {
        c[i].click();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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