我在表格中使用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>
发布于 2020-05-25 00:24:47
<script type=...出现问题,您需要向new jscolor(...注册新输入。这是一个有效的代码
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jscolor/2.0.4/jscolor.min.js"></script>
</head>
<body>
<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');
colourCell.appendChild(colourWidget);
newRow.appendChild(colourCell);
tableBody.appendChild(newRow);
new jscolor(colourWidget);
}
</script>
</body>
</html>https://stackoverflow.com/questions/61988645
复制相似问题