简体   繁体   English

当前元素之前和之后具有SAME CLASS的所有元素的CSS选择器

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

相关问题 选择器出现在元素之后的所有元素 - Selector for all elements that appear after an element CSS 后代选择器获取具有特定属性的所有元素,忽略具有相同属性的元素的任何后代 - CSS descendent selector to get all elements with a certain attribute, ignoring any that are descendents of an element with that same attribute 之前如何选择同一类的所有元素 - How to select all elements of the same class before 在 css 中的选择器不显示在所有元素和 JQquery 错误之后 - After selector in css doens´t show in all elements and JQquery error 通过选择器 getElementByClassName 选择具有相同类的所有元素未按预期工作 - Select all elements with same class by the selector getElementByClassName not working as expected 隐藏具有相同类的所有元素,然后添加具有相同类 Javascript 的元素 - Hide All Elements with Same Class Then Add An Element With Same Class Javascript 带有类的所有按钮的CSS选择器 - CSS Selector for all the buttons with class 选择具有与当前事件目标相同的类名称的所有元素 - Select all elements with same class names as the current event target 选择一个css选择器来动态设置元素的样式:: before - choosing a css selector for dynamically styling an element ::before 在单击时,仅将类应用于单击的元素,而不是将所有元素都应用于同一类 - On click only apply class to element clicked, not all elements with the same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM