EventListener for a class?

后端 未结 4 361
离开以前
离开以前 2021-01-17 07:15

I have an EventListener that references an Id and it works well, the only problem is that i have at least a dozen places where this EventListener needs to reference so i don

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-17 07:20

    If you're not using jQuery you can use querySelectorAll:

    var els = document.querySelectorAll(".chartJump");
    
    
    for (var i = 0; i < els.length; i++) {
    
        els[i].addEventListener("click", function (e) {
            e.preventDefault()
    
            eventHandler();
        });
    };
    

    If you've jQuery you can use on:

    $(".chartJump").on('click', function () {
        // Your code
    });
    

提交回复
热议问题