[英]Selecting parent element without any of its children in CSS and also in native Javascript
I would like to select only the parent element without any of its children. 我只想选择父元素,而不选择其任何子元素。 Not sure how to do this;
不确定如何执行; I have experimented with the
:not()
pseudo-class to no avail. 我已经尝试过
:not()
伪类,但没有用。
In this case, I am doing this because I want to reduce the brightness of the parent of an accordion on hover. 在这种情况下,我这样做是因为我想降低悬停时手风琴的父级的亮度。
Without attempting to isolate the parent, it is something like: 无需尝试隔离父级,它类似于:
.parent:hover {
filter: brightness(85%);
}
However, this will cause all of its children to darken as well. 但是,这也会导致其所有子项也变暗。 As I also have some later
.parent
classes inside of .parent
classes, mousing over them will double the effect. 由于我也有一些后来
.parent
的内部类.parent
类,鼠标放在他们会加倍的效果。
Instead, I would like ONLY the parent to be darkened. 相反,我只想让父母变黑。 How can I achieve this?
我该如何实现?
Note: I have not gotten anything like .parent:hover:not(.parent *)
to work. 注意:我还没有像
.parent:hover:not(.parent *)
无法正常工作。
These .parent
elements are also accordions that work based off of toggling an active
class on them via a click listener in Javascript. 这些
.parent
元素也是手风琴,它们基于通过Javascript中的单击侦听器在其上切换active
类而起作用。 Of course, this means that I activate the accordion on the .parent
element when clicking on its children in the same manner that the brightness works. 当然,这意味着我在单击
.parent
元素的.parent
元素时以与亮度起作用相同的方式激活了手风琴。
So how can I either a) add the click listener to elements with the .parent
class and not their children, or b) check if a child element in the .parent
-classed element was clicked on to trigger the listener? 因此,我如何a)将单击侦听器添加到具有
.parent
类而不是其子元素的元素中,或b)检查是否单击了.parent
-classed元素中的子元素来触发侦听器?
Current code: 当前代码:
var accordians = document.getElementsByClassName("accordian");
var i;
for (i = 0; i < accordians.length; i++) {
accordians[i].addEventListener("click", function() {
var acc = this;
acc.classList.toggle("active");
// sitelist-container elements are "expanded" when the accordions are clicked
var sitelist = acc.getElementsByClassName("sitelist-container")[0];
if (sitelist.style.display == "") {
sitelist.style.display = "block";
} else {
sitelist.style.display = "";
}
});
}
Any help for the CSS or for the Javascript would be appreciated. CSS或Javascript的任何帮助将不胜感激。
EDIT: Thank you for the feedback about using the background color instead of a brightness filter. 编辑:感谢您提供有关使用背景色而不是亮度滤镜的反馈。 It actually looks better overall when using this strategy.
使用此策略时,总体上看起来更好。
However, I still have an issue... how can I make it so that this styling only takes place when I hover over the parent element and not its children elements? 但是,我仍然有一个问题……我该如何做到只有当我将鼠标悬停在父元素而不是其子元素上时才能进行此样式设置? Is this at all possible, or should I fundamentally redesign how I am attacking this problem?
这是完全有可能的,还是我应该从根本上重新设计解决这个问题的方式?
For your CSS, Unfortunately, you are unable to filter the parent without filtering the child. 对于您的CSS, 不幸的是,您无法不过滤子级就过滤父级。 So, we will reflect on a similar problem with
background-color
instead. 因此,我们将思考一个类似的
background-color
问题。
If you style on a parent element, it is styling the parent element, and because the default background-color is transparent, it seems like the rest of the elements have the same color, but they are not . 如果您在父元素上设置样式,则是在对父元素进行样式设置,并且由于默认的background-color是透明的,因此看起来其余元素的颜色相同,但实际上不是 。 However, by adjusting the default or simply setting the same property to a different value, you will see the problem resolved.
但是,通过调整默认值或简单地将相同属性设置为其他值,您将看到问题已解决。 Take a look at the following example:
看下面的例子:
div * { background-color: white; } div { background-color: red; border: 1px solid black; }
<div> <p> This is a paragraph about life. Life is 42. This is from the Hitchhiker's guide to the Galaxy. </p> <p> This is another paragraph. </p> </div>
Without the div *
rule, the background-color is by default transparent. 如果没有
div *
规则,则默认情况下background-color是透明的。 You can think of setting the div *
or *
as resetting the default. 您可以考虑将
div *
或*
设置为重置默认值。 Then, only your parent selector is changing, and the children stay the same. 然后,只有您的父选择器正在更改,而子代保持不变。
There is another solution, that's a little wacky, but it works. 还有另一种解决方案,这有点古怪,但是可以。 You could code two
div
s that are siblings under the body
instead of a child-parent relationship and use positioning to position the second div inside the first. 您可以将两个
div
编码为body
下的兄弟姐妹,而不是父子关系,并使用定位将第二个div放置在第一个div内。 Here is a working fiddle that represents this. 这是一个工作的小提琴,代表了这一点。 http://jsfiddle.net/404_Error/td13ophq/
http://jsfiddle.net/404_Error/td13ophq/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.