Problema javascript rating

Discutii si intrebari legate de scripturi si functii JavaScript, jQuery si Ajax, cod JavaScript in general.
GarryOne
Mesaje:27

Problema javascript rating

Acesta este link-ul cu problema:
URL: scantodefend.com/muller008/testimonials.html

Acesta este codul javascript:

Cod: Selectaţi tot

var sMax;	
var holder;
var preSet;
var rated;
function rating(num){
    sMax = 0;
    for(n=0; n<num.parentNode.childNodes.length; n++){
        if(num.parentNode.childNodes[n].nodeName == "A"){
            sMax++;	
        }
    }

    if(!rated){
        s = num.id.replace("_", ''); // Get the selected star
        a = 0;
        for(i=1; i<=sMax; i++){		
            if(i<=s){
                document.getElementsByClassName("_"+i).className = "on";
                document.getElementsByClassName("rateStatus").innerHTML = num.title;	
                holder = a+1;
                a++;
            }else{
                document.getElementsByClassName("_"+i).className = "";
            }
        }
    }
}
function off(me){
    if(!rated){
        if(!preSet){	
            for(i=1; i<=sMax; i++){		
                document.getElementsByClassName("_"+i).className = "";
                document.getElementsByClassName("rateStatus").innerHTML = me.parentNode.title;
            }
        }else{
            rating(preSet);
            document.getElementsByClassName("rateStatus").innerHTML = document.getElementsByClassName("ratingSaved").innerHTML;
        }
    }
}
function rateIt(me){
    if(!rated){
        document.getElementsByClassName("rateStatus").innerHTML = document.getElementsByClassName("ratingSaved").innerHTML + me.title;
        preSet = me;
        rated=1;
        rating(me);
    }
}
Acesta este codul html;

Cod: Selectaţi tot

<span class="rateStatus"></span>
                            <span class="ratingSaved"></span>
                            <div class="rateMe">
                                <a onclick="rateIt(this)" class="_1" onmouseover="rating(this)" onmouseout="off(this)"></a>
                                <a onclick="rateIt(this)" class="_2" onmouseover="rating(this)" onmouseout="off(this)"></a>
                                <a onclick="rateIt(this)" class="_3" onmouseover="rating(this)" onmouseout="off(this)"></a>
                                <a onclick="rateIt(this)" class="_4" onmouseover="rating(this)" onmouseout="off(this)"></a>
                                <a onclick="rateIt(this)" class="_5" onmouseover="rating(this)" onmouseout="off(this)"></a>
                            </div>
Scriptul initial lucra cu id-uri, dar mie imi trebuie pentru mai multe posturi, deci am incercat sa-l fac sa lucreze cu clase, dar nu mi-a reusit. Ma puteti ajuta va rog.
RADIO Club http://radio-klub.info" target="_blank

MarPlo Mesaje:4343
Salut
Din ce vad in script, e gresit folosita metoda getElementsByClassName(). Aceasta returneaza un set de elemente care au acea classa, adica un array cu acele elemente.
Nu merge:

Cod: Selectaţi tot

document.getElementsByClassName("rateStatus").innerHTML
Deoarece " document.getElementsByClassName("rateStatus") " contine un array cu elementele ce au class="rateStatus", si la array nu se aplica innerHTML .
Merge ceva de genul:

Cod: Selectaţi tot

document.getElementsByClassName("rateStatus")[i].innerHTML
- Unde " i " e numarul de ordine /indexul.

Ar trebui refacut tot script-ul. De folos e sa folosesti Addon-ul Firebug in Mozila Firefox, iti indica erorile din codul JavaScript.
- Sau poti folosi script-ul de aici: Script notare /rating cu stelute.

Subiecte similare