繁体   English   中英

插入 <select>使用innerHTML将按钮导入HTML表格

[英]Inserting <select> button into HTML table using innerHTML

我试图在关联的Javascript函数中使用.innerHTML将输入按钮插入到HTML表中。

每当我使用相同的JS,但将“select”替换为其他HTML时,它的工作完全正常。 只有<input type =“button”/>和<button>不起作用。

该问题至少出现在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" />";
};

我尝试用格式化文本(“<b>文本</ b>”)和链接替换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”变得复杂。

任何帮助,将不胜感激!

此行将触发“意外标识符”错误:

cell.innerHTML = "<input id="Button" type="button" value="ClickHere" />";

问题是你的报价。 JavaScript解释器与用于表示JavaScript中的字符串的引号和HTML中使用的引号相混淆。 您需要转义字符串中的引号,或者使用单引号作为其中一个目的。

单引号可行:

cell.innerHTML = '<input id="Button" type="button" value="ClickHere" />';

或者,转义引号将起作用:

cell.innerHTML = "<input id=\"Button\" type=\"button\" value=\"ClickHere\" />";

我想你正在寻找<input type="button" />不选择也许? select元素用于下拉菜单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM