탐색 모음을 만들고 작동시키고 있습니다. 그러나 나는 내 글을 바꾸고 싶다. 다음과 같습니다 반복적 인 코드가있다 :자바 스크립트, 이것을 루프로 변경할 수 있습니까?
$("nav span").mouseover(function(){
var currentColor = $("#" +$(this).attr("data-loc")).css("backgroundColor");
var currentPos = $(this).position().left;
if ($(this).attr("data-loc") === "home"){
$("#hoverstyle").animate({"left":currentPos},150);
$("#hoverstyle").css("backgroundColor",currentColor);
$("#hoverstyle").css({width:$(this).width()});
}
else if($(this).attr("data-loc") === "writer"){
$("#hoverstyle").animate({"left":currentPos},150);
$("#hoverstyle").css("backgroundColor",currentColor);
$("#hoverstyle").css({width:$(this).width()});
}
else if($(this).attr("data-loc") === "historian"){
$("#hoverstyle").animate({"left":currentPos},150);
$("#hoverstyle").css("backgroundColor",currentColor);
$("#hoverstyle").css({width:$(this).width()});
}
else if($(this).attr("data-loc") === "teacher"){
$("#hoverstyle").animate({"left":currentPos},150);
$("#hoverstyle").css("backgroundColor",currentColor);
$("#hoverstyle").css({width:$(this).width()});
}
else if($(this).attr("data-loc") === "fencing"){
$("#hoverstyle").animate({"left":currentPos},150);
$("#hoverstyle").css("backgroundColor",currentColor);
$("#hoverstyle").css({width:$(this).width()});
}
});
하지만 그것은 더 나은 내가 루프 만들기로 생각하는 방법을 알아낼 수 없습니다,하지만 난 -.- 그것을 알아낼 수 없습니다. . 도와주세요!
왜 '스위치'를 사용하지 않습니까? – Cilan
왜'if' /'else if'를 사용하고 있습니까? 모든 상황에서 똑같은 일을하고있는 것입니다. –
왜 다른 사람을 추가했을 때 그들에 별도의 아무것도하지 않을 때 –