290290 text-align : left; /* 編碼文字左對齊 */
291291 min-height : 16px ; /* 確保編碼行的高度 */
292292 }
293+
294+ /* 新增的顏色樣式 */
295+ .correct-char {
296+ color : green;
297+ }
298+
299+ .incorrect-char {
300+ color : red;
301+ }
293302
294303 /* 鍵盤樣式 */
295304 .keyboard-container {
@@ -716,15 +725,18 @@ <h2>查碼練習</h2>
716725 updateStateAndDisplay ( ) ;
717726 } else {
718727 deleteTextAtCursor ( textOutput ) ;
728+ updatePhraseLookup ( ) ; // 在刪除 text-output 的文字後更新比對
719729 }
720730 return ;
721731 }
722732 if ( code === 'enter' ) {
723733 insertTextAtCursor ( textOutput , '\n' ) ;
734+ updatePhraseLookup ( ) ; // 在換行後更新比對
724735 return ;
725736 }
726737 if ( code === 'escape' ) {
727738 resetInputState ( ) ;
739+ updatePhraseLookup ( ) ;
728740 return ;
729741 }
730742
@@ -733,6 +745,7 @@ <h2>查碼練習</h2>
733745 selectCandidate ( 0 ) ;
734746 } else {
735747 insertTextAtCursor ( textOutput , ' ' ) ;
748+ updatePhraseLookup ( ) ; // 在空格後更新比對
736749 }
737750 return ;
738751 }
@@ -754,24 +767,29 @@ <h2>查碼練習</h2>
754767 // 處理功能鍵
755768 if ( char === 'backspace' ) {
756769 deleteTextAtCursor ( textOutput ) ;
770+ updatePhraseLookup ( ) ;
757771 return ;
758772 }
759773 if ( char === 'enter' ) {
760774 insertTextAtCursor ( textOutput , '\n' ) ;
775+ updatePhraseLookup ( ) ;
761776 return ;
762777 }
763778 if ( char === 'escape' ) {
764779 resetInputState ( ) ;
780+ updatePhraseLookup ( ) ;
765781 return ;
766782 }
767783 if ( char === ' ' ) {
768784 insertTextAtCursor ( textOutput , ' ' ) ;
785+ updatePhraseLookup ( ) ;
769786 return ;
770787 }
771788
772789 // 處理英文字母和數字、符號
773790 if ( char . length === 1 ) {
774791 insertTextAtCursor ( textOutput , char ) ;
792+ updatePhraseLookup ( ) ;
775793 }
776794 }
777795
@@ -801,9 +819,11 @@ <h2>查碼練習</h2>
801819 if ( currentCandidates . length === 1 ) {
802820 insertTextAtCursor ( textOutput , currentCandidates [ 0 ] ) ;
803821 resetInputState ( ) ;
822+ updatePhraseLookup ( ) ; // 在自動選字後更新比對
804823 return ;
805824 } else if ( currentCandidates . length === 0 ) {
806825 resetInputState ( ) ;
826+ updatePhraseLookup ( ) ;
807827 return ;
808828 }
809829 }
@@ -844,6 +864,7 @@ <h2>查碼練習</h2>
844864 if ( selectedCandidate ) {
845865 insertTextAtCursor ( textOutput , selectedCandidate ) ;
846866 resetInputState ( ) ;
867+ updatePhraseLookup ( ) ; // 在選字後更新比對
847868 }
848869 }
849870
@@ -1016,6 +1037,7 @@ <h2>查碼練習</h2>
10161037 // --- 新增的批量查碼處理邏輯 ---
10171038 function updatePhraseLookup ( ) {
10181039 const textToSearch = phraseInput . value ;
1040+ const textToCompare = textOutput . value ;
10191041 encodingResults . innerHTML = '' ;
10201042
10211043 const characters = textToSearch . split ( '' ) . slice ( 0 , 10 ) ;
@@ -1028,6 +1050,23 @@ <h2>查碼練習</h2>
10281050 const charLabel = document . createElement ( 'span' ) ;
10291051 charLabel . className = 'char-label' ;
10301052
1053+ // 判斷字元是否比對正確,並設定對應的顏色
1054+ if ( i < characters . length ) {
1055+ const char = characters [ i ] ;
1056+ charLabel . textContent = char ;
1057+
1058+ if ( i < textToCompare . length ) {
1059+ if ( char === textToCompare [ i ] ) {
1060+ charLabel . classList . add ( 'correct-char' ) ;
1061+ } else {
1062+ charLabel . classList . add ( 'incorrect-char' ) ;
1063+ }
1064+ } else {
1065+ // 如果 text-output 比較短,則未輸入的字元顯示為紅色
1066+ charLabel . classList . add ( 'incorrect-char' ) ;
1067+ }
1068+ }
1069+
10311070 const encodingContainer = document . createElement ( 'div' ) ;
10321071 encodingContainer . style . display = 'flex' ;
10331072 encodingContainer . style . flexDirection = 'column' ;
@@ -1036,7 +1075,6 @@ <h2>查碼練習</h2>
10361075 const codesToDisplay = [ ] ;
10371076 if ( i < characters . length ) {
10381077 const char = characters [ i ] ;
1039- charLabel . textContent = char ;
10401078 const results = currentReverseCodemap [ char ] ;
10411079 if ( results && results . length > 0 ) {
10421080 codesToDisplay . push ( ...results . slice ( 0 , 5 ) ) ;
@@ -1072,10 +1110,6 @@ <h2>查碼練習</h2>
10721110 }
10731111 encodingContainer . appendChild ( codeLabel ) ;
10741112 }
1075-
1076- if ( i >= characters . length ) {
1077- charLabel . textContent = '' ;
1078- }
10791113
10801114 column . appendChild ( charLabel ) ;
10811115 column . appendChild ( encodingContainer ) ;
@@ -1084,6 +1118,7 @@ <h2>查碼練習</h2>
10841118 }
10851119
10861120 phraseInput . addEventListener ( 'input' , updatePhraseLookup ) ;
1121+ textOutput . addEventListener ( 'input' , updatePhraseLookup ) ;
10871122
10881123 // 頁面載入時先初始化顯示
10891124 switchInputMethod ( 'primary' ) ;
0 commit comments