You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加for循环缺失大括号后仍出现Uncaught SyntaxError错误求助

排查「Uncaught SyntaxError: Unexpected end of input」错误

你遇到的错误根源是代码存在多处未闭合的语法结构,补全for循环的大括号只是其中一步,还需要补全以下缺失的闭合部分:

  • else代码块的闭合大括号
  • form.addEventListener的箭头函数回调的闭合大括号
  • form.addEventListener调用的闭合括号与分号

以下是修正后的完整代码:

const form = document.getElementById('form');
form.addEventListener('submit', (event) => {
  // 阻止表单默认提交行为
  event.preventDefault();

  // 获取表单中的原文和副本文本
  const originalText = form.elements.originalText.value.trim();
  const copy = form.elements.copy.value.trim();

  // 对比原文和副本
  if (originalText === copy) {
    alert('两段文本完全一致!');
  } else {
    // 展示两段文本的差异
    const differencesDiv = document.getElementById('result');
    differencesDiv.innerHTML = '';

    // 将文本分割为句子数组
    const originalSentences = originalText.split('. ');
    const copySentences = copy.split('. ');

    // 创建表格元素
    const table = document.createElement('table');
    table.classList.add('differences-table');

    // 创建表头行
    const titlesRow = document.createElement('tr');
    const originalTitleCell = document.createElement('th');
    originalTitleCell.innerText = '原文';
    const copyTitleCell = document.createElement('th');
    copyTitleCell.innerText = '新版本';

    // 将表头单元格添加到表头行
    titlesRow.appendChild(originalTitleCell);
    titlesRow.appendChild(copyTitleCell);

    // 将表头行添加到表格
    table.appendChild(titlesRow);

    // 逐句对比文本
    for (let i = 0; i < originalSentences.length; i++) {
      // 创建句子行
      const row = document.createElement('tr');

      // 创建原文和副本句子的单元格
      const originalCell = document.createElement('td');
      originalCell.innerHTML = originalSentences[i];
      const copyCell = document.createElement('td');
      copyCell.innerHTML = copySentences[i];

      // 设置API端点和密钥
      const apiEndpoint = 'https://api.webspellchecker.net/v2/spell-check-text';
      const apiKey = 'MY API KEY';

      // 检查原句中的拼写错误
      fetch(apiEndpoint, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'X-Api-Key': apiKey
        },
        body: `text=${encodeURIComponent(originalSentences[i])}`
      })
      .then(response => response.json())
      .then(data => {
        // data.result.spellCheck.errors包含原句中的拼写错误数组
        data.result.spellCheck.errors.forEach(error => {
          // 为错误单词添加高亮样式
          originalCell.innerHTML = originalCell.innerHTML.replace(error.word, `<span class="different">${error.word}</span>`);
        });
      });

      // 将单元格添加到行
      row.appendChild(originalCell);
      row.appendChild(copyCell);

      // 将行添加到表格
      table.appendChild(row);
    } // for循环闭合括号

    // 将生成的表格添加到结果容器(原代码遗漏此步骤)
    differencesDiv.appendChild(table);
  } // else代码块闭合括号
}); // 回调函数及addEventListener调用闭合

额外补充:原代码中生成的表格没有添加到结果容器differencesDiv中,修正后的代码补上了这一步,确保对比结果能正常显示在页面上。

内容的提问来源于stack exchange,提问作者Nes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 05:51:29