[英]when click on next button or previous button at the same time active next li or provious li element by using javascript
when I click on the next or previous buttons the list items need to active at the same time.当我单击下一个或上一个按钮时,列表项需要同时激活。 if I click on next button the next list item needs to active (means color or background color) by using Javascript or typescript in angualr6.
如果我单击下一个按钮,则需要在 angualr6 中使用 Javascript 或 typescript 激活下一个列表项(表示颜色或背景颜色)。 thanks in advance.
提前致谢。
Previous Next前面一个后面一个
var list=document.getElementById("list").childElementCount; var count = 0; function activeclass(index){ parent = document.getElementById("list"); count += index; chailditem = parent.children[count]; chailditem.style.color = 'red'; if(count == list){ count == 0 } }
.active{ color:red; font-size:16px; }
<ul id="list"> <li class="item active">item 1<li> <li class="item">item 2<li> <li class="item">item 3<li> <li class="item">item 4<li> <ul> <button class="btn btn-default button-next" on-click="activeclass(-1)" type="button">Next</button> <button class="btn btn-default button-prev" on-click="activeclass(1)" type="button">Previous</button>
First of all it's not the decent solution, however you get an idea from here.首先,这不是一个不错的解决方案,但是您可以从这里得到一个想法。
var count = 0; function activeclass(val){ var totalElem=document.getElementsByClassName("item"); if(count > totalElem.length || count < 0) return; var list=document.getElementsByClassName("active"); let elem; if(val == 'next') { elem = list[0].nextElementSibling; count++; } else { elem = list[0].previousElementSibling; count--; } list[0].classList.remove("active"); elem.classList.add('active'); }
.active{ color:red; font-size:16px; }
<ul id="list"> <li class="item active">item 1</li> <li class="item">item 2</li> <li class="item">item 3</li> <li class="item">item 4</li> <ul> <button class="btn btn-default button-next" onclick="activeclass('next')" type="button">Next</button> <button class="btn btn-default button-prev" onclick="activeclass('prev')" type="button">Previo
Please don't hesitate to let me know if you have any query.如果您有任何疑问,请随时告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.