简体   繁体   English

更改伪类:在TD的javascript中的CSS之前

[英]changing pseudo class :before css in javascript for TD

I have a css class for td as follows: 我有一个TD的CSS类如下:

td {
    text-align: left;
    padding: 3px;
    color:white;
    background-color:#E3F2ED ;
    position:relative; 
    z-index:10; 
    border:1px solid #74827D;
    border-style:solid none;

}

 td:before{
    content:""; 
    position:absolute; 
    z-index:-1; 
    top:2px; 
    left:2px; 
    right:2px; 
    bottom:2px; 
    background:#20936C;
} 

This makes the individual cells with color #20936C and a border of color #E3F2ED and last border of color #74827D . 这将使单个单元格的颜色为#20936C ,边界为#E3F2ED ,最后一个边界为#74827D

Now I want to change the color of one cell when it is clicked. 现在,我想更改单击一个单元格时的颜色。 So basically I just want to change the :before color to let's say red. 所以基本上我只想将:before颜色更改为红色。

I think you can just use JS to add or remove a "clicked" class to those elements, then add some CSS for that class: 我认为您可以只使用JS向这些元素添加或删除"clicked"类,然后为该类添加一些CSS:

 td.clicked:before{
    background:#FF0000;
 }

The cascading nature of CSS means the main styles of your td:before selector will be applied, then the td.clicked:before style will override the background . CSS的级联性质意味着将应用td:before选择器的主要样式,然后td.clicked:before样式将覆盖background

So something like this: 所以像这样:

 document.querySelector("table").addEventListener("click", function(e) { if (e.target.nodeName === "TD") e.target.classList.toggle("clicked") }) 
 td { text-align: left; padding: 3px; color:white; background-color:#E3F2ED ; position:relative; z-index:10; border:1px solid #74827D; border-style:solid none; } td:before{ content:""; position:absolute; z-index:-1; top:2px; left:2px; right:2px; bottom:2px; background:#20936C; } td.clicked:before{ background:#FF0000; } 
 <table> <tr><td>Click me</td></tr> <tr><td>Or me</td></tr> </table> 

Idea is to add the css 想法是添加css

td.active:before {
  background: red;
}

and when clicked add the class active in the td 并在单击时将活动类添加到td中

 $(function() { $('td').on('click', function() { $(this).toggleClass('active') }); }); 
 td { text-align: left; padding: 3px; color: white; background-color: #E3F2ED; position: relative; z-index: 10; border: 1px solid #74827D; border-style: solid none; } td:before { content: ""; position: absolute; z-index: -1; top: 2px; left: 2px; right: 2px; bottom: 2px; background: #20936C; } td.active:before { background: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>This is an example</td> <td>This is an example</td> </tr> </table> 

The following toggles a "selected" class as other answers have, but I think it's better to use event delegation and put the listener on the table rather than every cell. 下面的代码会像其他答案一样切换“选择的”类,但是我认为最好使用事件委托并将侦听器放在表而不是每个单元格上。 Also, this solution uses plain JS, no additional library required. 此外,此解决方案使用纯JS,无需其他库。

Instead of using the tag name, you could add a "selectable" class to elements that are selectable and filter on that so it's not restricted to a particular tag name. 除了使用标签名称,您还可以在可选择的元素上添加“可选”类并对其进行过滤,因此它不仅限于特定的标签名称。

If you need support for browsers that don't support the classList property, that's not difficult. 如果您需要支持不支持classList属性的浏览器,这并不困难。 There are plenty of class add/remove/toggle utilities available. 有很多可用的类添加/删除/切换实用程序。

 window.onload = function(){ document.getElementById('table0').addEventListener('click', toggleCell, false); } function toggleCell(e) { // Get the element that was clicked on var tgt = e.target; // If it was a TD, toggle the "selected" class if (tgt.tagName.toLowerCase() == 'td') { tgt.classList.toggle('selected'); } } 
 .selected { background-color: red; } td { padding: 10px;} 
 <table id="table0"> <tr><td>a<td>b<td>c <tr><td>a<td>b<td>c <tr><td>a<td>b<td>c </table> 

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

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