我正在尝试创建一个脚本,用于在鼠标悬停事件中更改元素的重复背景图像。不幸的是它不能正常工作。我已经找到了几种使用 JavaScript 执行此操作的可能方法,但没有一种方法对我有用。我该如何解决这个问题?
以下代码无法正常工作:
while (document.getElementById("content_" + modid + "_" + i) != null) {
document.getElementById("content_" + modid + "_" + i).style.display = "none";
document.getElementById("menu_" + modid + "_" + i).style.backgroundImage = "url(psycho_normal.jpg)";
document.getElementById("menu_" + modid + "_" + i).style.backgroundPosition = "top left";
document.getElementById("menu_" + modid + "_" + i).style.backgroundRepeat = "repeat-x";
i++;
}
document.getElementById("menu_" + modid + "_" + ind).style.backgroundImage = "url(phycho_hover.jpg)";
document.getElementById("menu_" + modid + "_" + ind).style.backgroundPosition = "top left";
document.getElementById("menu_" + modid + "_" + ind).style.backgroundRepeat = "repeat-x";
但是如果我尝试使用 backgroundColor 属性,它工作正常:
while (document.getElementById("content_" + modid + "_" + i) != null) {
document.getElementById("content_" + modid + "_" + i).style.display = "none";
document.getElementById("menu_" + modid + "_" + i).style.backgroundColor = "#000000";
i++;
}
document.getElementById("menu_" + modid + "_" + ind).style.backgroundColor = "#ff0000";
最佳答案
编写一个 CSS 类并像这样在 JavaScript 中调用它
document.getElementById("menu_" + modid + "_" + i).className = "yourcssclass"
看看会发生什么。
关于javascript - 使用 JavaScript 更改背景重复图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1906741/