jquery - 根据选定的下拉菜单设置 td 单元格的背景颜色
问题描述
我的代码:
<table>
<tr>
<td>
<select onchange="this.style.backgroundColor=this.options[this.selectedIndex].style.backgroundColor">
<option value=""></option>
<option value="1" style="background-color:yellow">One</option>
<option value="2" style="background-color:red">Two</option>
<option value="3" style="background-color:green">Three</option>
</select>
</td>
</tr>
</table>
现在在这里我可以在下拉框中设置选项的背景颜色。我需要的是使用选定的选项颜色设置单元格的背景颜色。我在互联网上搜索了很多,但找不到类似的东西。小提琴
解决方案
您将颜色应用于选择元素。相反,您应该将其应用于 select 元素的父级。我将 .parentNode 添加到 javascript 中。
td {
padding: 1rem; /* Just to make the color better visible */
}
<table>
<tr>
<td>
<select onchange="this.parentNode.style.backgroundColor=this.options[this.selectedIndex].style.backgroundColor;this.style.backgroundColor=this.options[this.selectedIndex].style.backgroundColor">
<option value=""></option>
<option value="1" style="background-color:yellow">One</option>
<option value="2" style="background-color:red">Two</option>
<option value="3" style="background-color:green">Three</option>
</select>
</td>
</tr>
</table>
推荐阅读
- azure - SSAS Invoke-ASCmd 创建或更新不创建数据库
- java - switch 语句未正确执行
- java - 移入列的算法:移动字母不起作用
- c# - 自定义 Slack 命令返回状态 200 并触发另一个作业
- mysql - Postfix 与 PostfixAdmin 和 MySQL 后端表查找/权限被拒绝错误
- sql - 在 30 秒内加入时间戳?
- javascript - 为什么 Swift 不能识别 JS 发送的数组?
- mysql - 从查询中只选择一列:“SHOW TABLE STATUS FROM `DB`”?
- php - 不存在的类:codeigniter 中的 CI_ApiClient 错误
- c# - 在自定义方法中为数组中的元素分配最小值 (array.Min())