我想知道如何更改活动页面的颜色?该功能无法正常工作,我真的不想将其设置为输入type =“ button” ...,因为它看起来更糟。我在这里想念什么?
<form1>
<script>
function btnColor(btn, color) {
var property = document.getElementById(btn)
property.style.backgroundColor = color;
}
</script>
<div class="pagination">
<a id="pageOne" onclick="btnColor('pageOne','#ffcce9');">1</a>
<a id="pageTwo" onclick="btnColor('pageTwo','#ffcce9');">2</a>
<a id="pageThree" onclick="btnColor('pageThree','#ffcce9');">3</a>
</div>
</form1>
最佳答案
让我们尝试一下(在html中单击事件不是一个好习惯)
<form1>
<div class="pagination">
<a id="pageOne">1</a>
<a id="pageTwo">2</a>
<a id="pageThree">3</a>
</div>
</form1>
<script>
links = document.querySelectorAll("a")
links.forEach(function (item) {
item.addEventListener('click', function () {
//reset the color of other links
links.forEach(function (item) {
item.style.backgroundColor = '#fff'
})
// apply the style to the link
this.style.backgroundColor = '#ffcce9'
});
})
</script>