弹出 window 有奇怪的文字

Popup window has strange text

我有一个有 2 个电池的 table;一个有数据,另一个没有。我想为用户提供一种通过隐藏的 div、“TextEditor”编辑该数据的方法。

function editText(textID) {
  var cText = document.getElementById(textID).innerHTML;
  document.getElementById('editBox').innerHTML = cText;
  toggleHide('TextEditor');
}

function toggleHide(id) {
  var x = document.getElementById(id);
  x.style.display = x.style.display === "none" ? "block" : "none";
}
#testTable td {
  width: 50px;
  height: 50px;
  border: black solid 1px;
}

#TextEditor {
  width: 200px;
  height: 100px;
  background-color: blue;
  display: none;
  left: 300px;
  margin-top: 30px;
}

#editBox {
  width: 95%;
  height: 95%;
}
<table id="testTable" border="1px">
  <tr>
    <th>Note 1</th>
    <th>Note 2</th>
  </tr>
  <tr>
    <td onclick="editText('text1')">
      <div id="text1"></div>
    </td>
    <td onclick="editText('text2')">
      <div id="text2">abcdefghijk</div>
    </td>
  </tr>
</table>
<div id="TextEditor">
  <textarea id="editBox" name="editBox"></textarea>
  <input type="button" value="Cancel" onclick="toggleHide('TextEditor')" />
</div>

在 table 单元格内部单击会显示 TextEditor div,其中包含可供编辑的单元格文本。试试点击第二个单元格看看。

单击“取消”关闭第一个弹出窗口,单击第一个单元格。键入一些乱码,例如“adsadg”,然后单击“取消”。现在单击单元格 #2 - 带有文本的单元格和文本丢失但显示乱码。单步执行代码显示单元格 2 的文本已被清楚地检索和保存,但只有乱码显示。我做错了什么?

解决方案是对 textarea 使用 .value 而不是 .innerHTML,因为它是一个表单元素。我测试了它,它确实解决了问题。你的 javascript 就变成了

function editText(textID) {
  var cText = document.getElementById(textID).innerHTML;
  document.getElementById('editBox').value = cText; // <<<< VALUE!!
  toggleHide('TextEditor');
}

function toggleHide(id) {
  var x = document.getElementById(id);
  x.style.display = x.style.display === "none" ? "block" : "none";
}

此代码还有很多其他问题,例如切换,但这确实解决了您遇到的问题。

我找到了解决方案 here

我正在检查您的代码,我不确定 objective 是什么,但对其进行了一些更改:

const textAreaId = "editBox";
const textEditor = "TextEditor";
var editorOpened = false;
var activeCell = "text1";

function editText(textID) {
  // You change the cell id to update it later
  activeCell = textID;
  // Get the text from the cell
  const currentText = document.getElementById(textID).innerHTML || "";
  // Set the current text to the textarea
  document.getElementById(textAreaId).value = currentText;
  // Show the textarea to update the cell
  toggleHide("TextEditor");
}

function toggleHide() {
  editorOpened = !editorOpened;
  document.getElementById(textEditor).style.display = editorOpened
    ? "block"
    : "none";
}

function updateTableCell() {
  const inputText = document.getElementById(textAreaId).value;
  document.getElementById(activeCell).innerHTML = inputText;
}
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
    <style type="text/css">
      #testTable td {
        width: 50px;
        height: 50px;
        border: black solid 1px;
        cursor: pointer;
      }
      #TextEditor {
        width: 200px;
        height: 100px;
        background-color: blue;
        display: none;
        left: 300px;
        margin-top: 30px;
      }
      #editBox {
        width: 95%;
        height: 95%;
      }
    </style>
  </head>
  <body>
    <table id="testTable" border="1px">
      <tr>
        <th>Note 1</th>
        <th>Note 2</th>
      </tr>
      <tr>
        <td onclick="editText('text1')"><div id="text1"></div></td>
        <td onclick="editText('text2')"><div id="text2">abcdefghijk</div></td>
      </tr>
    </table>
    <div id="TextEditor">
      <textarea
        id="editBox"
        name="editBox"
        onkeyup="updateTableCell()"
      ></textarea>
      <input type="button" value="Close" onclick="toggleHide()" />
    </div>
    <script src="PageTest.js" type="text/javascript"></script>
  </body>
</html>

如果您愿意,可以查看它,如果您需要任何帮助,请告诉我。

我也尝试找到解决您问题的方法并更改了您的代码:

//I made the variables global to be able to get both elements on both functions
var cText = document.getElementById("text2");
var text = document.getElementById('editBox');

//On this function it gets the text on the table cell and put it on the editBox
function editText(){
    text.innerText = cText.innerText
    toggleHide('TextEditor');
   }

//On this function when the cancel button is clicked the table cell receives the value on the 'editBox'
function toggleHide(id) {
    var x = document.getElementById(id);
     x.style.display = x.style.display === "none" ? "block" : "none";

     cText.innerText = text.value
}