有没有办法在document.write()中获取大量代码?

时间:2014-10-21 14:56:37

标签: javascript html function document.write

例如,我想得到这样的东西:在document.write

<form>
            <table>

            <tr>
              <td align="right">Subtotal:</td>
              <td align="left"><form name = "e" id ="e"><input type="text" id="subtotal" /></td>
            </tr>

            <tr>
              <td align="right">Sales Tax:</td>
              <td align="left"><form name = "f" id = "f"><input type="text" id="tax" /></td>
            </tr>

            <tr>
              <td align="right">Total:</td>
              <td align="left"><form name = "g" id = "g"><input type="text" id="total" /></td>
            </tr>

          </table>
    </form>

我试过像

这样的东西
document.write("paste whole code here");

没有工作。

1 个答案:

答案 0 :(得分:1)

对于记录,我不鼓励使用document.write。但要回答你的问题,你只需要转义字符串中的换行符:

    document.write('<form> \
                <table> \
                <tr> \
                  <td align="right">Subtotal:</td> \
                  <td align="left"><form name = "e" id ="e"><input type="text" id="subtotal" /></td> \
                </tr> \
                <tr> \
                  <td align="right">Sales Tax:</td> \
                  <td align="left"><form name = "f" id = "f"><input type="text" id="tax" /></td> \
                </tr> \
                <tr> \
                  <td align="right">Total:</td> \
                  <td align="left"><form name = "g" id = "g"><input type="text" id="total" /></td> \
                </tr> \
              </table> \
        </form>');