简体   繁体   English

我正在尝试使用javascript更改表单元格的属性,并且找到了我的建议,但似乎无法使它们正常工作

[英]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.

相关问题 我似乎无法让我的Javascript表单验证正常工作 - I can't seem to get my Javascript form validation to work 我似乎无法让我的悬停效果在按钮上工作。 尝试了很多技巧 - i can't seem to get my hover effects to work on button. have tried many tips 设置间隔(); 不工作。 我正在尝试倒计时到 go,但它卡住了,我似乎找不到它有什么问题 - setInterval(); not working. I am trying to get my countdown to go down but it gets stuck and I can't seem to find whats wrong with it 我似乎无法使我的代码正常工作 - I can't seem to get my code to work 我似乎无法让我的 React 私人路线工作 - I can't seem to get my React private route to work 我似乎无法让我的代码显示我的计算,我是否缺少 output 标签? - I can't seem to get my code to display my calculation, am I missing an output tag? 我似乎无法在 eventDrop 和 drop 上获取 resourceId,我正在使用 resourceTimelineMonth 并试图移动它们 - I can't seem to get resourceId on eventDrop and drop, im using resourceTimelineMonth and are trying to move them around 我无法处理JavaScript冲突。 您应该如何检查它们? - I can't get my JavaScript collisions to work. How should you check for them? 我有一个画布,我想在上面生成随机矩形。 我似乎无法正常工作 - I have a canvas that I'd like to generate random rectangles on. I can't seem to get it to work 我正在尝试使用JavaScript创建测验,但我的按钮无法正常工作 - I'm trying to create a quiz with JavaScript and I can't get my button to work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM