在javascript的var中集成html
我有一个简单的html假设在javascript的var中集成html,javascript,jquery,html,Javascript,Jquery,Html,我有一个简单的html假设 <table> <tbody> <tr> <td> <span>Customer Type </span> </td> <td> <input id="TxtCustomerName" name="TxtCustomerName" type="text"> </td> </tr> </tbody> </table>
<table>
<tbody>
<tr>
<td>
<span>Customer Type </span>
</td>
<td>
<input id="TxtCustomerName" name="TxtCustomerName" type="text">
</td>
</tr>
</tbody>
</table>
现在,我正试图在窗口中生成一个html弹出窗口作为
myWindow=window.open('','','width=200,height=100')
var html="<body><table><tbody><tr>";
html+="<td><span>Customer Type</span></td>";
html+="<td><input id="TxtCustomerName" name="TxtCustomerName" type="text"></td>"
html+="</tr></tbody></table><body>";
alert(html);
myWindow.document.write(html)
myWindow.focus()
但这场战争并没有出现。此外,由于控件输入文本,此错误正在发生。
帮助我在html变量中集成控件,以便弹出窗口显示控件。您的引号被弄乱了。尝试:
html+="<td><input id='TxtCustomerName' name='TxtCustomerName' type='text'></td>";
字符串中的引号前面应加反斜杠。这允许JavaScript解释器将字符串中的引号与用作字符串分隔符的引号区分开来。下面是一个例子:
string1='It\'s five o\'clock!';
string2="<A HREF=\"index.htm\">";
string1="It's five o'clock!";
string2='<A HREF="index.htm">';
或者,如果字符串仅包含单引号,则可以使用双引号作为字符串分隔符,反之亦然。下面是一个例子:
string1='It\'s five o\'clock!';
string2="<A HREF=\"index.htm\">";
string1="It's five o'clock!";
string2='<A HREF="index.htm">';
参考资料:看看JS语法是如何突出显示的。TxtCustomerName不是您所期望的,因为它正在结束字符串。应该是
html+="<td><input id='TxtCustomerName' name='TxtCustomerName' type='text'></td>";
使用
您需要转义引号,但忘记了结尾冒号: html+=;
缺少双引号以转义
选中此项只是一个问题-但为什么要创建一个新窗口,而不是一个模式假设您也可以使用jQuery和jQuery UI,document.write是不受欢迎的。不过,您仍然可以使用innerHtml:去掉并使用myWindow.document.body.innerHtml=html;
html+="<td><input id='TxtCustomerName' name='TxtCustomerName' type='text'></td>"