使用复选框将行设置为'variable',然后按下按钮设置单元格的格式,可以通过以下步骤完成:
<input>
元素来创建复选框,设置type
属性为checkbox
。addEventListener
方法来实现,将事件类型设置为click
,并指定一个回调函数。querySelectorAll
方法来获取所有复选框元素,选择器可以是input[type="checkbox"]
。checked
属性来判断复选框是否被选中。以下是一个示例代码:
<!DOCTYPE html>
<html>
<head>
<style>
.variable {
background-color: yellow;
}
</style>
</head>
<body>
<table>
<tr>
<td><input type="checkbox"></td>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>Cell 1</td>
<td>Cell 2</td>
</tr>
</table>
<button id="formatButton">设置格式</button>
<script>
const formatButton = document.getElementById('formatButton');
formatButton.addEventListener('click', function() {
const checkboxes = document.querySelectorAll('input[type="checkbox"]');
checkboxes.forEach(function(checkbox) {
if (checkbox.checked) {
const row = checkbox.parentNode.parentNode;
row.classList.add('variable');
}
});
});
</script>
</body>
</html>
在上述示例中,当点击"设置格式"按钮时,被选中的复选框所在的行将被设置为黄色背景,表示其格式已被设置为'variable'。
请注意,上述示例仅为演示目的,实际应用中可能需要根据具体需求进行修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云