2012-06-08 3 views
1

나는JQuery와 찾기 및 바꾸기 여러 항목

뭔가 이런 각각의 언어에 대한 번역 할 필요가 매달 테이블이 원래 사이의 매핑을 유지할 수

$('.lang-en #monthly th').each(function() { 
    var text = $(this).text(); 
    $(this).text(text.replace('Tam', 'Jan')); 
    $(this).text(text.replace('Hel', 'Feb')); 
    $(this).text(text.replace('Maa', 'Mar')); 
    $(this).text(text.replace('Huh', 'Apr')); 
    $(this).text(text.replace('Tou', 'May')); 
    $(this).text(text.replace('Kes', 'Jun')); 
    $(this).text(text.replace('Hei', 'Jul')); 
    $(this).text(text.replace('Elo', 'Aug')); 
    $(this).text(text.replace('Syy', 'Sep')); 
    $(this).text(text.replace('Lok', 'Oct')); 
    $(this).text(text.replace('Mar', 'Nov')); 
    $(this).text(text.replace('Jou', 'Dec')); 
    $(this).text(text.replace('Yht', 'Total')); 

}); 
+0

잘 보이는, 정확히'나던 작업 obviously'에 의해 무엇을 의미합니까? – Sarfraz

+0

"네가 분명히 일하지 않는다"고 하던데, 어떻게 그렇게? 왜이게 효과가 없습니까? 이 코드 스 니펫의 핵심은 무엇입니까? –

+0

사이드 노트 @client로'$ (this)'캐싱을 고려하십시오. 그렇게하는 것이 가장 좋습니다. –

답변

2

(을 분명히 나던 작업) 대체 문자열, 그리고 text()에 함수를 전달합니다

var mapping = { 
    "Tam": "Jan", 
    "Hel": "Feb", 
    // ...and so on... 
}; 

$("#monthly th").text(function(index, originalText) { 
    return mapping[originalText]; 
}); 

편집 : 당신이 다시하려면 텍스트의 일부만을 배치, 당신은 오히려 객체보다 중첩 된 배열을 사용할 수 있습니다

var mapping = [ 
    ["Tam", "Jan"], 
    ["Hel", "Feb"], 
    // ...and so on... 
]; 

$("#monthly th").text(function(index, originalText) { 
    var pattern = mapping[index]; 
    return originalText.replace(pattern[0], pattern[1]); 
}); 
+0

어쩌면 그는 'th'에 다른 텍스트가있을 수 있습니다. 'Tou :'또는''Huh : ssa'. – VisioN

+0

@VisioN, 좋은 지적입니다. 그에 따라 내 대답을 업데이트 할 것입니다. 감사합니다 :) –

+0

감사합니다 프레데릭 – client