[英]I am trying to change the properties of a table cell with javascript and have found my suggestions but I can't seem to get them to work
I have a table where each row has 7 input columns. 我有一个表,其中每行有7个输入列。 I want to disable the 3rd column of each row and change the text color to red.
我想禁用每一行的第三列,并将文本颜色更改为红色。
function changeCells() {
var table = document.getElementById("tbl");
for (var i = 0; i < table.rows.length; i++) {
var tr = table.getElementsByTagName("tr")[i];
var td = tr.getElementsByTagName("td")[2];
td.style.color = 'red';
td.disabled = true;
}
}
I can change the background with: 我可以使用以下方法更改背景:
td.style.backgroundColor = 'red';
but not the text color. 但不是文字颜色。
The input column is also not disabled. 输入列也未禁用。 Not sure why this code doesn't work.
不知道为什么这段代码不起作用。
Without seeing the rest of the CSS and HTML, it's difficult to tell, but it sounds like you have other CSS setting the text color (assuming the background color changes works as you described). 没有看到其余的CSS和HTML,很难分辨,但是听起来您还有其他CSS设置了文本颜色(假设背景颜色的更改如您所描述的那样起作用)。 You can add the
!important
modifier via Javascript to test this for certain: 您可以通过Javascript添加
!important
修饰符,以进行以下测试:
td.style.setProperty("color", "red", "important");
If it takes that, then something else wasn't letting you change the color. 如果需要的话,那么别的什么都不允许您更改颜色。 Ideally, you would instead add a class to the
<td>
element, and then you can control what it looks like via pure CSS. 理想情况下,您可以将类添加到
<td>
元素,然后可以通过纯CSS控制其外观。
In JS: 在JS中:
td.className = 'highlight-cell';
Then in CSS: 然后在CSS中:
.highlight-cell {
color: red !important;
}
A td
element cannot be disabled. 不能禁用
td
元素。 You need to target the input
element within it. 您需要定位其中的
input
元素。 Here is a working example of what you want: https://jsfiddle.net/0xk99dxr/ 这是您想要的工作示例: https : //jsfiddle.net/0xk99dxr/
And the relevant code being added: 并添加了相关代码:
var inputElement = td.querySelector("input");
inputElement.style.color = 'red';
inputElement.disabled = true;
To change the text color inside every row of third column, use the following line of code in css file. 要更改第三列每一行内的文本颜色,请在css文件中使用以下代码行。
#tbl tr td:nth-child(3){color: red}
Table cells don't have a disabled property. 表格单元格没有禁用的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.