JavaScript中的HTML代码无法使用document.write

时间:2018-08-31 08:16:26

标签: javascript html document.write

我只在html中尝试过此代码,它可以工作,但是在使用document.write时,它根本不执行。 notepad ++正在将代码变黑,直到img标签(包括img)为止。 我是新手,找不到错误。

代码:

<script type="text/javascript" language="javascript">
document.write("
<table valign="middle" width="100%" height="100%" border="0" cellpadding="15%" >
    <tr align="center" valign="middle" height="50%" >
        <td><img src="images/logo.png" ></td>
    </tr>
    <tr height="10%">
        <td colspan=2>
            <input type="text" id="eno" maxlength="9" size="9" placeholder="Enrollment No." style="border:0;font-size:72;width:100%;height:100%;">
        </td>
    </tr>
    <tr height="10%">
        <td colspan=2>
            <input type="button" value="Enter" onclick="SaveEno()" style="font-size:72;color:white;background-color:black;width:100%;height:100%;border:0;">
        </td>
    </tr>
    <tr>
        <td></td>
    </tr>
</table>
");
</script>

2 个答案:

答案 0 :(得分:1)

在这种情况下,document.write(“”)考虑了诸如换行符,双引号等标记的格式。由于它不是JavaScript字符串,因此请勿使用双引号。为此,请在document.write(``)函数中包含所有标记。欲了解更多https://www.w3schools.com/jsref/met_doc_write.asp

答案 1 :(得分:0)

HTML片段中的双引号干扰了JavaScript。除非您需要支持Internet Explorer,否则您实际上应该使用template literal。如果是这样,您要么必须在HTML片段周围使用单引号,要么在import datetime datetime.datetime.strptime('1/1/2016 0:00', "%m/%d/%Y %H:%M").strftime('%Y-%m-%d %H:%M:%S') 中将所有双引号转义。

\"