父元素的HTML DOM更改样式

Cur*_*ous 2 html javascript css

function cross() {
  var dik = document.getElementsByName('r1c1')[0].parentNode;
  dik.style.color = "red";
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

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

San*_*yal 5

您必须使用属性backgroundColor来更改的颜色tdcolor用于更改颜色text

在这里阅读颜色与背景颜色

<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>
Run Code Online (Sandbox Code Playgroud)