javascript - HTML DOM 更改父元素的样式

标签 javascript html css

function cross() {
  var dik = document.getElementsByName('r1c1')[0].parentNode;
  dik.style.color = "red";
}
<table>
  <tr>
    <td><input type="radio" name="r1c1" value="TRUE"></td>
    <td><input type="radio" name="r1c1" value="FALSE"></td>
  </tr>

  <tr>
    <td><input type="radio" name="r1c2" value="TRUE"></td>
    <td><input type="radio" name="r1c2" value="FALSE"></td>
  </tr>

  <input type="button" name="sbmt" value="CHECK" onclick="cross();">

</table>

当我点击按钮时,我想将 td block 的颜色更改为包含 输入元素 <input type="radio" name="r1c1" value="TRUE">

最佳答案

您必须使用属性 backgroundColor 来更改 td 的颜色。 color 用于改变text

的颜色

Read Here color vs backgroundColor

<script type="text/javascript">
function cross(){
    var dik = document.getElementsByName('r1c1')[0].parentNode;
    dik.style.backgroundColor = "red";
}
</script>

<table>
<tr>
    <td><input type="radio" name="r1c1" value="TRUE"></td>
    <td><input type="radio" name="r1c1" value="FALSE"></td>
</tr>

<tr>
    <td><input type="radio" name="r1c2" value="TRUE"></td>
    <td><input type="radio" name="r1c2" value="FALSE"></td>
</tr>

<input type="button" name="sbmt" value="CHECK" onclick="cross();">

</table>

关于javascript - HTML DOM 更改父元素的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47222970/

相关文章:

javascript - click() 上的 serialize() 时为空字符串

javascript - getImageData 返回上一张上传图片的值,而不是当前图片的值

javascript - 浏览器确认关闭: cancel button also closes the browser

html - 如何让标题和两个按钮与边框正确对齐?

css - @Media 没有像我想象的那样工作

javascript - 将 JavaScript 嵌入 XML 兼容的 JSP 文件 (JSPX)

javascript - 'form-data/multipart' 类型的 HTTPS 请求(使用 https nodejs 模块)不起作用

html - CSS :hover is not working in this code

javascript - 如何将 HTML、CSS 和 Javascript 组合成 HTML 代码

html - 将 <td> 中的文本输入与 twitter bootstrap 中的右侧对齐?