【问题标题】:document.write() is throwing syntax errordocument.write() 抛出语法错误
【发布时间】:2023-03-20 16:25:02
【问题描述】:

我试图使用 JavaScript 添加按钮,但不是将按钮添加到页面,而是抛出 SyntaxError: Invalid or unexpected token

document.write('

<p>Sign in</p>
    <form action="/chat" method="GET">
      <p> Login: <input type="text" name="login"/> </p>
        <p>   Password: <input type="text" name="password"/> <input type="submit" value="Ok"> </p>
    </form>
    <br>
    <form action="/signup">
        <button type = "submit"> button for registration</button>
    </form>');

【问题讨论】:

  • 一些基本的 JS 语法教程可能是有序的;这不是你做多行字符串的方式。也会有其他问题,但至少从一些语言基础开始。

标签: javascript html htmlbutton


【解决方案1】:

请不要以这种方式打印 HTML,但使用 ES6 back ticks 应该可以解决此问题。

document.write(`
    <p>Sign in</p>
    <form action="/chat" method="GET">
    	<p> Login: <input type="text" name="login"/> </p>
    	<p>   Password: <input type="text" name="password"/> <input type="submit" value="Ok"> </p>
    </form>
    <br>
    <form action="/signup">
    	<button type = "submit"> button for registration</button>
    </form>
`);

【讨论】:

    【解决方案2】:

    你不能在函数代码中输入,因为解释器认为这是新的代码行。 检查:

    document.write('<p>Sign in</p><form action="/chat" method="GET"><p> Login: <input type="text" name="login"/> </p><p>   Password: <input type="text" name="password"/> <input type="submit" value="Ok"> </p></form><br><form action="/signup"><button type = "submit"> button for registration</button</form>');
    

    【讨论】:

      猜你喜欢
      • 2016-10-31
      • 2019-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 2014-06-06
      • 1970-01-01
      相关资源
      最近更新 更多