我为我制作的一个 5 星评级系统编写了这个脚本,当用户将鼠标悬停在它们上方时,它会使星星亮起,例如:如果用户将鼠标悬停在第三颗星上,那颗星会显示点亮的星星图像以及前两个。该脚本适用于除 FireFox 之外的所有较新的浏览器。我已经为此绞尽脑汁了一段时间,无法弄清楚为什么它不能在 Firefox 中工作。任何帮助表示赞赏。此外,我只对原始 Javascript 的响应感兴趣,而不是 JQuery 或任何其他框架。谢谢一堆。
function btnSwap(){
var myBtns = document.getElementsByClassName('btns');
for(i=0; i<myBtns.length; i++){
var elem = document.getElementById(myBtns[i].id);
elem.src = "images/"+"starNorm.png";
elem.onmouseover = btnOver;
elem.onmouseout = btnOut;
function btnOver(){
var lit = document.getElementById(this.id);
if(lit.id == "rb1"){
lit.src = "images/"+"starOver.png";
}
if(lit.id == "rb2"){
lit.src = "images/"+"starOver.png";
document.getElementById('rb1').src = "images/"+"starOver.png";
}
if(lit.id == "rb3"){
lit.src = "images/"+"starOver.png";
document.getElementById('rb1').src = "images/"+"starOver.png";
document.getElementById('rb2').src = "images/"+"starOver.png";
}
if(lit.id == "rb4"){
lit.src = "images/"+"starOver.png";
document.getElementById('rb1').src = "images/"+"starOver.png";
document.getElementById('rb2').src = "images/"+"starOver.png";
document.getElementById('rb3').src = "images/"+"starOver.png";
}
if(lit.id == "rb5"){
lit.src = "images/"+"starOver.png";
document.getElementById('rb1').src = "images/"+"starOver.png";
document.getElementById('rb2').src = "images/"+"starOver.png";
document.getElementById('rb3').src = "images/"+"starOver.png";
document.getElementById('rb4').src = "images/"+"starOver.png";
}
}
function btnOut(){
var lit = document.getElementById(this.id);
if(lit.id == "rb1"){
lit.src = "images/"+"starNorm.png";
}
if(lit.id == "rb2"){
lit.src = "images/"+"starNorm.png";
document.getElementById('rb1').src = "images/"+"starNorm.png";
}
if(lit.id == "rb3"){
lit.src = "images/"+"starNorm.png";
document.getElementById('rb1').src = "images/"+"starNorm.png";
document.getElementById('rb2').src = "images/"+"starNorm.png";
}
if(lit.id == "rb4"){
lit.src = "images/"+"starNorm.png";
document.getElementById('rb1').src = "images/"+"starNorm.png";
document.getElementById('rb2').src = "images/"+"starNorm.png";
document.getElementById('rb3').src = "images/"+"starNorm.png";
}
if(lit.id == "rb5"){
lit.src = "images/"+"starNorm.png";
document.getElementById('rb1').src = "images/"+"starNorm.png";
document.getElementById('rb2').src = "images/"+"starNorm.png";
document.getElementById('rb3').src = "images/"+"starNorm.png";
document.getElementById('rb4').src = "images/"+"starNorm.png";
}
}
}
}