我制作了这个自定义语言替换器。但这是双向的。但是,当您第一次从第一个文本区域进行翻译时,就会发生问题。它在它旁边的文本区域内输出它。但是当我然后去删除输出中的文本时。它不会让我使用该文本区域来返回到自定义语言。如果我刷新页面,它将只允许这样做。我想避免这样做。
function TranslatetoUnseen() {
var Translation = document.getElementById("Unseen").value;
Translation = Translation.replace(/ㅏ/g, "A"); // if ㅏ then A
Translation = Translation.replace(/\s\s+/g, ' '); // when to many spaces
Translation = Translation.toLowerCase();
document.getElementById("English").innerHTML = Translation[0].toUpperCase() + Translation.slice(1);
}
function TranslatetoEnglish() {
var TranslationEng = document.getElementById("English").value;
TranslationEng = TranslationEng.replace(/A/g, "ㅏ"); // if A then ㅏ
TranslationEng = TranslationEng.replace(/a/g, "ㅏ"); // if a then ㅏ
TranslationEng = TranslationEng.replace(/\s\s+/g, ' '); // Adds 1 more space
document.getElementById("Unseen").innerHTML = TranslationEng;
}
<table align="center" class="center" width="50%">
<tr style="font-size: 150%;">
<th width="386">Unseen</th>
<th width="386">English</th>
</tr>
<tr>
<td><textarea id="Unseen" oninput=TranslatetoUnseen() size="4" wrap="hard" draggable="false"></textarea></td>
<td><textarea id="English" oninput=TranslatetoEnglish() size="4" wrap="hard" draggable="false"></textarea></span>
</td>
</tr>
</table>
当我在框内输入内容,然后想要相反时,它不允许这样做。什么时候应该允许它双向运行
答案 0 :(得分:2)
Translation[0].toUpperCase()
.value
代替innerHTML
function TranslatetoUnseen() {
var Translation = document.getElementById("Unseen").value;
Translation = Translation.replace(/ㅏ/g, "A"); // if ㅏ then A
Translation = Translation.replace(/\s\s+/g, ' '); // when to many spaces
Translation = Translation.toLowerCase();
document.getElementById("English").value = Translation.length === 0? "" : Translation[0].toUpperCase() + Translation.slice(1);
}
function TranslatetoEnglish() {
var TranslationEng = document.getElementById("English").value;
TranslationEng = TranslationEng.replace(/A/g, "ㅏ"); // if A then ㅏ
TranslationEng = TranslationEng.replace(/a/g, "ㅏ"); // if a then ㅏ
TranslationEng = TranslationEng.replace(/\s\s+/g, ' '); // Adds 1 more space
document.getElementById("Unseen").value = TranslationEng;
}
<table align="center" class="center" width="50%">
<tr style="font-size: 150%;">
<th width="386">Unseen</th>
<th width="386">English</th>
</tr>
<tr>
<td><textarea id="Unseen" oninput=TranslatetoUnseen() size="4" wrap="hard" draggable="false"></textarea></td>
<td><textarea id="English" oninput=TranslatetoEnglish() size="4" wrap="hard" draggable="false"></textarea></td>
</tr>
</table>