【发布时间】:2012-09-19 16:52:58
【问题描述】:
我正在尝试在关联的 Javascript 函数中使用 .innerHTML 将输入按钮插入 HTML 表中。
每当我使用相同的 JS,但将“选择”替换为其他 HTML 时,它都可以正常工作。只有 和
该问题至少出现在 IE、Firefox 和 Chrome 中。 Chrome 返回“意外标识符”错误。
缩写代码(不是原文):
function newrow() {
table = document.getElementById("Table");
row = table.insertRow(1);
cell = row.insertCell(0);
cell.innerHTML = "<input id="Button" type="button" value="ClickHere" />";
};
我已经尝试用格式化文本(“Text”)和链接替换HTML,并且都正确显示。
我还在脚本之外创建了对象,这确实有效,但似乎没有必要:
<input id="Button" type="button" value="ClickHere" />
<script type="text/javascript">
function newrow() {
table = document.getElementById("Table");
button = document.getElementById("Button");
row = table.insertRow(1);
cell = row.insertCell(0);
cell.innerHTML = button;
};
这行得通,但很笨拙,并且使我使用的任何“getindex”变得复杂。
任何帮助将不胜感激!
【问题讨论】:
-
带有 type="button" 的选择?
-
您知道"
-
不计算您发布的代码
cell.innerHTML = "<select id="Button" type="button" value="ClickHere" />";中的主要语法错误,按钮没有'type="select"`。
标签: javascript html html-table innerhtml