我是 JS 新手,正在从事一项需要:
当用户单击突出显示按钮时,将 css 类应用于每个其他 li 元素,这样您就可以交替使用颜色,并显示图像。
我无法让颜色更改的切换开关正常工作,我也无法显示图像。
<script>
var colors = new Array()
colors[0] = "#cc0099";
colors[1] = "#a4c400";
colors[2] = "#9900cc";
var counter = 0;
window.onload = function()
{
var highlightBtn= document.getElementById("highlight_btn");
highlightBtn.onclick = switchColors;
}
function switchColors()
{
var imgSrc = "images/" + [i];
var changeLI = document.getElementsByClassName("change");
changeLI.innerHTML = counter;
counter++;
}
</script>
<h1>Happy Mothers Day!</h1>
<h2>Things to get Mom:</h2>
<ul>
<li class="change">New Plants</li>
<li>Flowers</li>
<li class="change">A Day at the Spa</li>
<li>A Picture of Yourself</li>
<li class="change">New Shoes!</li>
<li>Anything with a Heart on It</li>
<li class="change"> Chocolate</li>
</ul>
<input id="highlight_btn" type="button" value="Highlight">