How to select element by 'role' attribute and filter by class with vanilla JS?

前端 未结 3 552
星月不相逢
星月不相逢 2021-02-06 08:48

Markup:

3条回答
  •  无人及你
    2021-02-06 09:27

    Try this:

    // remove active class from all elements
    document.querySelectorAll('[role="presentation"]').forEach(function (el){
    el.classList.remove("active");
    });
    
    // add class 'active' to last element
    document.querySelectorAll('[role="presentation"]:last-of-type')[0].classList.add("active")
    

    Notes:

    • 'classList' will not work in IE9;
    • I think you have to modify adding class row, depending on your needs.

提交回复
热议问题