-3
다음 코드는 이벤트가 발생했을 때 요소에있는 텍스트 값에 따라 클릭시 특정 요소의 텍스트를 변경합니다.Click on Click 이벤트 최적화하기
$('#left').on('click', function(){
if ($("#textContainer:contains('something')").length) {
$('#textContainer').text('third text replacement');
$('.elsewhere').text('more here');
}
else if ($("#textContainer:contains('third text replacement')").length) {
$('#textContainer').text('now the next item');
$('.elsewhere').text('something new here');
}
else if ($("#textContainer:contains('now the next item')").length) {
$('#textContainer').text('new text here');
$('.elsewhere').text('something else here');
}
else if ($("#textContainer:contains('new text here')").length) {
$('#textContainer').text('something');
$('.elsewhere').text('text here');
}
});
$('#right').on('click', function(){
if ($("#textContainer:contains('something')").length) {
$('#textContainer').text('new text here');
$('.elsewhere').text('something else here');
}
else if ($("#textContainer:contains('new text here')").length) {
$('#textContainer').text('now the next item');
$('.elsewhere').text('something new here');
}
else if ($("#textContainer:contains('now the next item')").length) {
$('#textContainer').text('third text replacement');
$('.elsewhere').text('more here');
}
else if ($("#textContainer:contains('third text replacement')").length) {
$('#textContainer').text('something');
$('.elsewhere').text('text here');
}
});
버전을 작업을위한 위의 바이올린을 참조하십시오.
더 쉽게 확장 할 수 있도록이 기능을 더 쉽게 관리 할 수있는 방법을 찾고 싶습니다. 이 사건을 처리 할 수있는 더 좋은 방법이 있습니까? 이를 함수로 압축하고 변수를 사용하여 #textContainer의 값을 저장하는 것이 더 이상적입니다. 어떤 제안?
나는'텍스트를 사용 http://codereview.stackexchange.com/ –
에 게시하는 것이 좋습니다 것 -> replacements'지도 또는 간단한 배열을 저장하기 위해 모든 텍스트를 읽고 그것을 회전합니다. –
이 질문은 http://codereview.stackexchange.com/ –