我怎样才能使用多种颜色进行这项工作?我也无法获得更改颜色的链接。
var div = document.getElementById('ColorChanger');
div.addEventListener('click', function(e){
var self = this,
old_bg = this.style.background;
document.body.style.background = document.body.style.background=='black'? 'white':'black';
document.body.style.color = document.body.style.color=='lime'? 'black':'lime';
document.alinkColor = document.linkcolor=='red'? 'black':'red';
})
<div id="ColorChanger">A+</div>
<a href="#">
Test
</a>
最佳答案
您可以像这样将所有颜色存储在一个数组中:
var div = document.getElementById('ColorChanger');
var allColors = [];
var currentColor = 0;
allColors.push({bg:"red",front:"green"});
allColors.push({bg:"green",front:"yellow"});
allColors.push({bg:"purple",front:"white"});
div.addEventListener('click', function(e){
var self = this,
old_bg = this.style.background;
document.body.style.background = allColors[currentColor].bg;
document.body.style.color = allColors[currentColor].front;
currentColor++;
if(currentColor == allColors.length) currentColor = 0;
})
要使其与链接一起使用,您应该使用 preventDefault。
关于javascript - 换色器仅适用于两种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46284315/