简体   繁体   English

如何使CSS样式覆盖JavaScript应用的样式

[英]How to make CSS styles override JavaScript applied styles

I create an HTML table dynamically, and apply some styles: 我动态创建一个HTML表,并应用一些样式:

var tbl = document.createElement('table');

tbl.id = "CrewMemberTable";

document.getElementById("CrewMemberPanel").appendChild(tbl);

I then insert rows and cells in the table (just three in this example for brevity): 然后我在表格中插入行和单元格(为简洁起见,在此示例中只有三个):

CrewRow = document.getElementById("CrewMemberTable").insertRow(-1);

CrewCell = CrewRow.insertCell(-1); CrewCell.innerHTML = "this text";
CrewCell = CrewRow.insertCell(-1); CrewCell.innerHTML = "some text";
CrewCell = CrewRow.insertCell(-1); CrewCell.innerHTML = "more text";

On this last cell, I apply a style, via javascript: 在最后一个单元格中,我通过javascript应用了一个样式:

CrewCell = CrewRow.insertCell(-1); CrewCell.innerHTML = "more text";
CrewCell.style.backgroundColor = "GREEN";

Finally, I have this style in my .CSS stylesheet: 最后,我在.CSS样式表中有这种风格:

#CrewMemberTable tr:hover {
    background-color: RED !important;
}

When I run it, strictly speaking, I guess nothing is wrong. 当我运行时,严格来说,我猜没有错。 It's doing what I asked it to do. 它正在做我要求它做的事情。

BUT - I want the hover to cover the entire row, but instead it only covers the first two cells, and the last cell, where the background color was assigned in javascript, it remains green. 但是 - 我希望悬停覆盖整行,但它只覆盖前两个单元格,而最后一个单元格,其中背景颜色在javascript中分配,它保持绿色。 I want the entire row red. 我希望整行变红。

Is this possible? 这可能吗? And how? 如何?

The problem is, you're only targeting the <tr> with CSS but the GREEN background is on the <td> . 问题是,你只是用CSS定位<tr>但是GREEN背景在<td>

Add this to your CSS 将其添加到您的CSS中

#CrewMemberTable tr:hover td {
  background-color: transparent !important;
}

Demo 演示

 #CrewMemberTable tr:hover { background-color: RED !important; } #CrewMemberTable tr:hover td { background-color: transparent !important; } 
 <div id="CrewMemberPanel"> <table id="CrewMemberTable"> <tr> <td>this text</td> <td>some text</td> <td style="background-color: GREEN">more text</td> </tr> </table> </div> 

You will need a selector with greater specificity, such as 您将需要一个具有更高特异性的选择器,例如

#CrewMemberTable tr:hover, #CrewMemberTable tr:hover td {
    background-color: RED !important;
}

You will particularly want to target the cell in addition to the row. 除了行之外,您还特别希望定位单元格。 (If you wanted you could add a class or id to the cell to facilitate higher-specificity targeting.) (如果您希望可以向单元格添加类或ID以促进更高特异性的定位。)

See snippet below for a working example using the code structure you have provided in your question: 使用您在问题中提供的代码结构,请参阅下面的代码段以获取工作示例:

 var tbl = document.createElement('table'); tbl.id = "CrewMemberTable"; document.getElementById("CrewMemberPanel").appendChild(tbl); CrewRow = document.getElementById("CrewMemberTable").insertRow(-1); CrewCell = CrewRow.insertCell(-1); CrewCell.innerHTML = "this text"; CrewCell = CrewRow.insertCell(-1); CrewCell.innerHTML = "some text"; CrewCell = CrewRow.insertCell(-1); CrewCell.innerHTML = "more text"; CrewCell = CrewRow.insertCell(-1); CrewCell.innerHTML = "more text"; CrewCell.style.backgroundColor = "GREEN"; 
 #CrewMemberTable tr:hover, #CrewMemberTable tr:hover td { background-color: RED !important; } 
 <div id="CrewMemberPanel"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM