Javascript不会打开包含表格和表单的弹出窗口

Javascript does not open popup window with table and form in it

本文关键字:表单 窗口 表格 包含 Javascript      更新时间:2023-09-26

只要表单包含任何元素,窗口就不会打开。但是,如果表单是空的(如果您从代码中删除),则弹出窗口工作正常。

Javascript:

function openScheduleWindow() {
    var scheduleWindow = window.open("", "Schedule Window", "width=700, height=200");
    scheduleWindow.document.write("<html><body><table border=1> '
        <tr><th>C1</th><th>C2</th></tr><tr><td>1</td><td>A</td></tr> '
        <tr><td>2</td><td>B</td></tr> '
        </table><form><input type="text" name="firstname"></form>");
}

您忘记转义HTML字符串中的"

 scheduleWindow.document.write("<html><body><table border=1> '
        <tr><th>C1</th><th>C2</th></tr><tr><td>1</td><td>A</td></tr> '
        <tr><td>2</td><td>B</td></tr> '
        </table><form><input type='"text'" name='"firstname'"></form>");

<input type="text" name="firstname">行上的双引号导致了问题,请尝试用单引号替换它们,应该可以正常工作

function openScheduleWindow() {
  var scheduleWindow = window.open("", "Schedule Window", "width=700, height=200");
  scheduleWindow.document.write("<html><body><table border=1> <tr><th>C1</th><th>C2</th></tr><tr><td>1</td><td>A</td></tr> <tr><td>2</td><td>B</td></tr> </table><form><input type='text' name='firstname'></form>");
}