我在表格中使用 jscolor 选择器。这在原始 HTML 中的一行中有效。但是,当我从 javascript 添加一行时,颜色选择器不起作用。检查创建的 HTML 时,输入标签是相同的。我认为用 jscolor js 注册这个元素一定有一些东西。
这是演示问题的最小 HTML:
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jscolor/2.0.4/jscolor.min.js"></script>
</head>
<button onclick="add_row_to_table()">Add Row</button>
<table>
<thead>
<tr><th>Colour</th></tr>
</thead>
<tbody id="tableBody">
<tr><td><input class="jscolor"></td></tr>
</tbody>
</table>
<script type="text/javascript">
function add_row_to_table(p) {
tableBody = document.getElementById('tableBody');
newRow = document.createElement('tr');
colourCell = document.createElement('td');
colourWidget = document.createElement('input');
colourWidget.setAttribute('class', 'jscolor');
colourWidget.setAttribute('autocomplete', 'off');
colourWidget.setAttribute('style', 'background-image: none, background-color: rgb(255, 255, 255); color: rgb(0, 0, 0);');
colourCell.appendChild(colourWidget);
newRow.appendChild(colourCell);
tableBody.appendChild(newRow);
}
</script>