简体   繁体   中英

how to add active class on anchor tag when clicked?

This is the javascript code in which I am getting anchor tags in loop. I want to add an active class on that anchor tag which is clicked or which is current page

function changePage(page)
{
  current_page = page;
  var page_span = document.getElementById("page");
  var number_of_pages = Math.ceil(videoData.length / records_per_page);
  var PageNum = "";
  for (var i = 1; i <= number_of_pages; i++) {
  PageNum += '<a class="page_link" href="javascript:changePage(' + i +')">'+ i +'</a> ';  //Page nos.
  }
 page_span.innerHTML =  PageNum;
}

HTML

Page: <span id='page'></span></div>
function onClick(e){
    e.preventDefault()
    document.querySelector('.active-class-name').classList.remove('active-class-name')
    e.target.classList.add('active-class-name')
}

<a onclick="onClick">Hi</a>

think this will work for you

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