내 질문에 맞는 코드를 마지막으로 조합했습니다. here. 그러나 특정 해시가있는 페이지를 새로 고치고 해당 탭을 클릭하여 동일한 페이지에 액세스 할 때 별도의 기능을 만들었 기 때문에 오랫동안 나타납니다.비슷한 기능을 수행 할 때 자바 스크립트 단순화
$(document).ready(function() {
$(function() {
var loc = window.location.href; // For when Hazel is refreshed
if (/Hazel/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("r p c").addClass("h");
$("#tab2").removeClass("tail");
$("#tab3, #tab4").addClass("tail");
}
});
$(function() {
var loc = window.location.href; // For when Red is refreshed
if (/Red/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("h p c").addClass("r");
$("#tab3, #tab2").removeClass("tail");
$("#tab4").addClass("tail");
}
});
$(function() {
var loc = window.location.href; // For when Pink is refreshed
if (/Pink/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("h r c").addClass("p");
$("#tab3, #tab4").removeClass("tail");
$("#tab2").addClass("tail");
}
});
});
$(function() {
var loc = window.location.href; // For when Cyan is refreshed
if (/Cyan/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("h r p").addClass("c");
$("#tab4").removeClass("tail");
$("#tab3, #tab2").addClass("tail");
}
});
$("#tab1").click(function() {
$(window).bind("hashchange", function() {
var loc = window.location.href; // For when Hazel tab is clicked
if (/Hazel/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("r p c").addClass("h");
$("#tab2").removeClass("tail");
$("#tab3, #tab4").addClass("tail");
}
});
});
$("#tab2").click(function() {
$(window).bind("hashchange", function() {
var loc = window.location.href; // For when Red tab is clicked
if (/Red/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("h p c").addClass("r");
$("#tab3, #tab2").removeClass("tail");
$("#tab4").addClass("tail");
}
});
});
$("#tab3").click(function() {
$(window).bind("hashchange", function() {
var loc = window.location.href; // For when Pink tab is clicked
if (/Pink/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("h r c").addClass("p");
$("#tab3, #tab4").removeClass("tail");
$("#tab2").addClass("tail");
}
});
});
$("#tab4").click(function() {
$(window).bind("hashchange", function() {
var loc = window.location.href; // For when Cyan tab is clicked
if (/Cyan/.test(loc)) {
$("#tab1,#tab2,#tab3,#tab4").removeClass("h r p").addClass("c");
$("#tab4").removeClass("tail");
$("#tab3, #tab2").addClass("tail");
}
});
});
});
간소화 할 수 있습니까? 나는 시도했지만 지금까지 시도한 바에 따르면 코드가 깨졌습니다.
네, 있습니다. 무엇을 시도 했습니까? 코드를 알려주십시오. – Bergi
왜 다른 $ (document) .ready?에 처음 3 개의 addOnDOMready-statements를 래핑 했습니까? – Bergi
이것은 오히려 http://codereview.stackexchange.com/에 속합니다. – Christoph