我正在尝试创建一个脚本,用于在mouseover事件中更改元素的重复背景图像。不幸的是它不能正常工作。我发现了几种可能的方式来使用JavaScript做到这一点,但它们都没有为我工作。我怎么解决这个问题?用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";
你确定该文件名为'phycho_hover.jpg'?看起来像一个错字 – 2009-12-15 11:17:49
是的,图像的名称是正确的。 – ktsixit 2009-12-15 11:20:17
也许你可以发布一些更多的代码?您的内容和菜单项如何在HTML中看起来如何?您如何调用onmouseover代码? – 2009-12-15 11:28:40