简体   繁体   English

如何在每个单元格中使用唯一颜色突出显示on表格单元格

[英]how to highlight a table cell onClick with unique colors in each cell

In the fiddle below, you can click on any of the cells and they will change colors to those in td.highlighted in the CSS. 在下面的小提琴中,您可以单击任何单元格,它们会将颜色更改为CSS中td.highlighted中的颜色。 I want the highlighted color to be assigned inline instead and unique to each element. 我希望突出显示的颜色可以内联分配,并且每个元素都是唯一的。

https://jsfiddle.net/rvxnmz8r/7 https://jsfiddle.net/rvxnmz8r/7

this is the line that generates the style for each table element, and I think the main problem is that I am dumb with CSS. 这是为每个表元素生成样式的行,我认为主要的问题是我对CSS很愚蠢。 Thanks for any help. 谢谢你的帮助。

var hstyle = 'style="td.highlighted {background-color: ' + '#'+Math.random().toString(16).substr(-6)  + '; color: black;}"';

update: the cells need to remain toggle-able between the default and custom highlighted colors. 更新:单元格需要在默认颜色和自定义突出显示颜色之间保持切换状态。

When using inline style combined with external CSS, the external need !important to override the inline style. 当使用内联样式与外部CSS结合使用时,外部需要!important是覆盖内联样式。

As a side note, using !important impact the usability of styling in a more difficult way to reuse, though, in your case, create 60 classes to toggle, I found more bad, hence the use of !important 作为旁注,使用!important影响样式的可用性以更难以重用的方式,但在您的情况下,创建60个类来切换,我发现更糟糕,因此使用!important

Change your script to 将脚本更改为

var hstyle = 'style="background-color: ' + '#' + Math.random().toString(16).substr(-6) + '; color: black;"';`

And your CSS to 而你的CSS

td.highlighted {
  background-color: blue !important;
  color: white !important;
}

Stack snippet 堆栈代码段

 var elements = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var transtable = [elements, elements, elements, elements, elements, elements]; var output = []; output.push('<table id="taxatable">'); for (var row = 0; row < transtable[0].length; row++) { output.push('<tr>'); for (var col = 0; col < transtable.length; col++) { var hclass = 'class="highlighted"'; var hstyle = 'style="background-color: ' + '#' + Math.random().toString(16).substr(-6) + '; color: black;"'; output.push( '<td ' + hclass + ' ' + hstyle + '>' + escape(transtable[col][row]) + '</td>' ); } output.push('</tr>'); } output.push('</table>'); document.getElementById('output').innerHTML = output.join(''); var tbl = document.getElementById("taxatable"); if (tbl != null) { for (var trow = 0; trow < tbl.rows.length; trow++) { for (var tcol = 0; tcol < tbl.rows[trow].cells.length; tcol++) { tbl.rows[trow].cells[tcol].onclick = function() { this.classList.toggle("highlighted"); }; //console.log(tbl.rows[trow].cells[tcol]); } } } 
 td { background-color: black; color: white; } td.highlighted { background-color: blue !important; color: white !important; } 
 <div id="output"> </div> 

Edit 编辑

I see what you're trying to do, but your solution is mixing two things together: inline styles and CSS rules. 我看到你正在尝试做什么,但你的解决方案是将两个东西混合在一起:内联样式和CSS规则。 You can only use style="" on an element to set styling directly on that element, which overrides the rules from your stylesheet. 您只能在元素上使用style=""直接在该元素上设置样式,这将覆盖样式表中的规则。 If you want to toggle the highlight class on and off you could do something like this (using jQuery): 如果你想打开和关闭highlight类,你可以做这样的事情(使用jQuery):

$("td").click(function() {
    $(this).toggleClass('highlighted');
});

Also, it needs the !important modifier in the highlighted class, as @LGSon explains above. 此外,它需要highlighted类中的!important修饰符,如上面的@LGSon所述。

In combination with my answer below (removing td.highlighted in the inline style), this should probably result in what you are looking for. 结合我在下面的回答(删除内联样式中的td.highlighted ),这可能会导致您正在寻找的内容。


Old answer 老答案

If you use inline styles, you are styling directly on the HTML element and you won't need to define a CSS selector. 如果使用内联样式,则直接在HTML元素上设置样式,您不需要定义CSS选择器。 Instead of 代替

var hstyle = 'style="td.highlighted {background-color...

you can simply write: 你可以简单地写:

var hstyle = 'background-color...'

So, your code becomes: 所以,你的代码变成:

var hstyle = 'style="background-color: ' + '#'+Math.random().toString(16).substr(-6)  + '; color: black;"';

You were close. 你很亲密 Remove the td.highlighted like so 像这样删除td.highlighted

var hstyle = 'style="background-color: ' + '#'+Math.random().toString(16).substr(-6)  + '; color: black;"';

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

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