Simek
(Gosimek)
2 Czerwiec 2008 19:00
#1
Mam problem z prostym, napisanym przeze mnie JS przeznaczonym do zmiany klasy elementu.
JS:
function changeclass(gdzie) {
if (gdzie == "1")
document.getElementById('home').className = 'clicked';
document.getElementById('projekty').style.className= 'no';
document.getElementById('omnie').style.className= 'no';
if (gdzie == "2")
document.getElementById('home').style.className= 'no';
document.getElementById('projekty').style.className= 'clicked';
document.getElementById('omnie').style.className= 'no';
if (gdzie == "3")
document.getElementById('home').style.className= 'no';
document.getElementById('projekty').style.className= 'no';
document.getElementById('omnie').style.className= 'clicked';
}
HTML:
[/code] CSS:
[code]#home .no { background: url(home_blue.png); } #home .clicked { background: url(home_blue_c.png); } #projekty .no { margin-left: 5px; background: url(projekty_blue.png); } #projekty .clicked { margin-left: 5px; background: url(projekty_blue_c.png); } #omnie .no { margin-left: 5px; background: url(omnie_blue.png); } #omnie .clicked { margin-left: 5px; background: url(omnie_blue_c.png); }
Problem jest w tym, że skrypt działa tylko dla “1”, czyli dla home. Nie jestem specjalistą od JS, więc po godzinnej walce z tym skryptem zwracam się do was z pomocą. Jeżeli suchy kod wam nie wystarcza mogę podać na PM link do wersji online.
Mam nadzieję, że ktoś mi pomoże! =P Pozdrawiam! ^^
function changeclass(gdzie) {
if (gdzie == 1)
{
document.getElementById('home').className = 'clicked';
document.getElementById('projekty').style.className= 'no';
document.getElementById('omnie').style.className= 'no';
}
if (gdzie == 2)
{
document.getElementById('home').style.className= 'no';
document.getElementById('projekty').style.className= 'clicked';
document.getElementById('omnie').style.className= 'no';
}
if (gdzie == 3)
{
document.getElementById('home').style.className= 'no';
document.getElementById('projekty').style.className= 'no';
document.getElementById('omnie').style.className= 'clicked';
}
}
Airborn
(Airborn)
2 Czerwiec 2008 19:15
#3
grupy instrukcji, jeżeli zajmują więcej niż jedną linię, należy grupować przy pomocy nawiasów klamrowych np.
function changeclass(gdzie) {
if (gdzie == "1"){
document.getElementById('home').className = 'clicked';
document.getElementById('projekty').style.className= 'no';
document.getElementById('omnie').style.className= 'no';
}
else if (gdzie == "2"){
document.getElementById('home').style.className= 'no';
document.getElementById('projekty').style.className= 'clicked';
document.getElementById('omnie').style.className= 'no';
}
else if (gdzie == "3"){
document.getElementById('home').style.className= 'no';
document.getElementById('projekty').style.className= 'no';
document.getElementById('omnie').style.className= 'clicked';
}
}
dodatkowo, konstrukcja z else if zamiast trzech osobnych ifów powinna działać sprawniej i szybciej
reszty co prawda nie sprawdzałem, ale to się rzuca w oczy na początku…
Simek
(Gosimek)
2 Czerwiec 2008 19:48
#4
Z przykrością muszę stwierdzić, że wypróbowałem oba podane rozwiązania, lecz sytuacja jest nadal taka sama.
function changeclass(gdzie) {
if (gdzie == 1)
{
document.getElementById('home').className = 'clicked';
document.getElementById('projekty').className= 'no';
document.getElementById('omnie').className= 'no';
}
if (gdzie == 2)
{
document.getElementById('home').className= 'no';
document.getElementById('projekty').className= 'clicked';
document.getElementById('omnie').className= 'no';
}
if (gdzie == 3)
{
document.getElementById('home').className= 'no';
document.getElementById('projekty').className= 'no';
document.getElementById('omnie').className= 'clicked';
}
}
Simek
(Gosimek)
3 Czerwiec 2008 09:22
#6
Działa bezbłędnie! Wielkie dzięki! ^^