我可以在javascript中使用innerHTML中的表单元素吗?

时间:2016-03-04 17:46:33

标签: javascript html

HEADER TAGS正在使用innerHTML:

document.getElementById ("x").innerHTML="<h1>something<h1>";

但表格元素不起作用!!

document.getElementById ("x").innerHTML="<input type="text">";

我想在某些条件下使用表单元素,如果单击单选按钮。

1 个答案:

答案 0 :(得分:2)

Look at your code closely:

document.getElementById ("x").innerHTML="<input type="text">";
// -------------------------------------^------------^----^-^

This is part of why JavaScript has always had two different kinds of quotes (now three, as of ES2015). So we can use ' for the string, and have " inside the string:

document.getElementById ("x").innerHTML='<input type="text">';

Although we could just escape the " if we wanted:

document.getElementById ("x").innerHTML="<input type=\"text\">";
// --------------------------------------------------^-----^