【问题标题】:JQuery: Uncaught SyntaxError: Invalid or unexpected tokenJQuery:未捕获的 SyntaxError:无效或意外的令牌
【发布时间】:2018-08-25 04:46:01
【问题描述】:

我也在使用 socket.io。有一个 HTML 表格,当用户单击按钮时,我的代码应该用新表格替换该表格,但是它在标题中给出了错误消息。

这是我的代码:
HTML:

<table>
                </tbody>
                    <tr>
                        <td class="1"></td>
                        <td class="2"></td>
                        <td class="3"></td>
                    </tr>
                    <tr>
                        <td class="4"></td>
                        <td class="5"></td>
                        <td class="6"></td>
                    </tr>
                    <tr>
                        <td class="7"></td>
                        <td class="8"></td>
                        <td class="9"></td>
                </tr>
                </tbody>
            </table>

jQuery 脚本:

socket.on('resetGranted', function() {
        $('table').replaceWith('<table>    //says error is here
                </tbody>
                    <tr>
                        <td class="1"></td>
                        <td class="2"></td>
                        <td class="3"></td>
                    </tr>
                    <tr>
                        <td class="4"></td>
                        <td class="5"></td>
                        <td class="6"></td>
                    </tr>
                    <tr>
                        <td class="7"></td>
                        <td class="8"></td>
                        <td class="9"></td>
                </tr>
                </tbody>
            </table>');
    })

我该如何解决这个问题?

【问题讨论】:

  • 尝试使用 .html() 而不是 .replaceWith()
  • 不要认为这是导致您的错误的原因,但请注意,您的 javascript 中的开始 tbody 标记实际上是一个结束标记,因此您可能需要修复它。跨度>

标签: javascript jquery html socket.io


【解决方案1】:

对多行字符串使用反引号`

console.log(`
    multi
    line
    string
    here
`);

【讨论】:

    【解决方案2】:
    socket.on('resetGranted', function() {
    var htmlContent='<table>
                </tbody>
                    <tr>
                        <td class="1"></td>
                        <td class="2"></td>
                        <td class="3"></td>
                    </tr>
                    <tr>
                        <td class="4"></td>
                        <td class="5"></td>
                        <td class="6"></td>
                    </tr>
                    <tr>
                        <td class="7"></td>
                        <td class="8"></td>
                        <td class="9"></td>
                </tr>
                </tbody>
            </table>';
            $('table').replaceWith(htmlContent);
        })
    

    【讨论】:

      猜你喜欢
      • 2016-10-17
      • 1970-01-01
      • 2017-05-02
      • 2020-10-13
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      相关资源
      最近更新 更多