最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery: Uncaught SyntaxError: Invalid or unexpected token - Stack Overflow

programmeradmin1浏览0评论

I am also using socket.io. There is an HTML table, and when a user clicks a button, my code is supposed to replace that table with a new one, however it gives the error message in the title.

Here is my code:
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 script:

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>');
    })

How do I fix this?

I am also using socket.io. There is an HTML table, and when a user clicks a button, my code is supposed to replace that table with a new one, however it gives the error message in the title.

Here is my code:
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 script:

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>');
    })

How do I fix this?

Share Improve this question asked Mar 16, 2018 at 7:31 J DuhJ Duh 1611 silver badge12 bronze badges 2
  • Try using .html() instead of .replaceWith() – Del Commented Mar 16, 2018 at 7:34
  • Don't think this is the cause of your error but note that what should be the opening tbody tag in your javascript is actually a closing tag, so you probably want to fix that. – cjl750 Commented Mar 16, 2018 at 7:34
Add a ment  | 

2 Answers 2

Reset to default 9

Use backtick ` for multiline string

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

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);
    })
发布评论

评论列表(0)

  1. 暂无评论