[英]CSS selector for ALL elements with SAME CLASS after and before current element
I was looking here: CSS Selector for selecting an element that comes BEFORE another element? 我在这里看: CSS Selector用于选择另一个元素之前的元素? ...but wasn't able to find a correct answer for my issue. ...但是无法为我的问题找到正确的答案。
Here is a fiddle example: https://jsfiddle.net/Munja/bm576q6j/3/ 这是一个小提琴的例子: https : //jsfiddle.net/Munja/bm576q6j/3/
.test:hover, .test:hover + .test
With this, when I :hover element with .test class, I achieved to change style for current element with .test class and first next element with .test class. 有了这个,当我:使用.test类悬停元素时,我实现了使用.test类更改当前元素的样式,并使用.test类更改第一个下一个元素。
What am I trying to achieve? 我想要实现的目标是什么?
When I select any row/column (eg agent 2), I want to apply same style for ALL elements with that same class ( .test in this case). 当我选择任何行/列(例如代理2)时,我想对具有相同类的所有元素应用相同的样式(在这种情况下为.test )。
If it is not possible to achieve this with css only, * I am willing to accept and other good solution.* 如果不能用css实现这一点, *我愿意接受和其他好的解决方案。*
Thank you. 谢谢。
In your specific case you can use 在您的具体情况下,您可以使用
tbody:hover > .test {
background: green;
}
fiddle: https://jsfiddle.net/bm576q6j/4/ 小提琴: https : //jsfiddle.net/bm576q6j/4/
Note that if you add more classes in the same tbody
it will not give what you want. 请注意,如果您在同一个tbody
添加更多类,则无法提供您想要的内容。 Check also this question: Hover on element and highlight all elements with the same class 还要检查这个问题:将鼠标悬停在元素上并突出显示具有相同类的所有元素
So, after waiting for several more hours, I have decided to use JavaScript solution mentioned in answer from @BasvanStein. 所以,等了几个小时之后,我决定使用@BasvanStein回答中提到的JavaScript解决方案。 Posting it here as answer, to make things easier for someone else with same issue. 将其作为答案发布在此处,以便让具有相同问题的其他人更轻松。
Here is a working fiddle: https://jsfiddle.net/Munja/bm576q6j/15/ 这是一个工作小提琴: https : //jsfiddle.net/Munja/bm576q6j/15/
var elms = document.getElementsByClassName("test");
var n = elms.length;
function changeColor(color) {
for(var i = 0; i < n; i ++) {
elms[i].style.backgroundColor = color;
}
}
for(var i = 0; i < n; i ++) {
elms[i].onmouseover = function() {
changeColor("red");
};
elms[i].onmouseout = function() {
changeColor("white");
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.