简体   繁体   English

JS DOM操作如何影响CSS规则匹配?

[英]How does JS DOM manipulation affect CSS rule matching?

Here is the setup: dynamically generates content for a number of items which need to be displayed together inside a single div. 这里是设置:动态生成需要在单个div中一起显示的多个项目的内容。 The layout is: An outer div (the container for everything), several inner divs (content varies), and a div inside each other inner div, along with some text. 布局是:一个外部div(所有内容的容器),几个内部div(内容各不相同),以及每个内部div内的一个div,以及一些文本。

The outermost div is part of the page from the beginning, and the js gets it by id. 最外面的div从一开始就是页面的一部分,而js通过id获取它。 Next each inner div and its content are generated and then appended to the outer div. 接下来,将生成每个内部div及其内容,然后将其附加到外部div。 Rinse, repeat. 冲洗,重复。

The outer div has a class that matches it in css. 外部div具有一个与CSS匹配的类。 The inner div is matched as: 内部div匹配为:

.outerdiv div{...

now... what actually happens is: both the inner div AND the content div match the outerdiv div rule, despite that one of them is clearly (triple checked in an inspector, Firefox AND Chrome) a child of the inner div not the outer. 现在...实际发生的是:内部div和内容div都与外部div规则匹配,尽管其中之一显然(在检查器中,Firefox和Chrome进行了三重检查)是内部div的子级,而不是外部div的子级。

I have this in two separate places so I'm fairly sure I'm not crazy. 我在两个不同的地方都有这个,所以我很确定自己没有疯。 Can somebody tell me how CSS rules are evaluated when DOM manipulations happen? 有人可以告诉我在发生DOM操作时如何评估CSS规则吗? (or where to go to find out?) (或去哪里找?)

Thanks, -Conrad 谢谢,-康拉德

The .outerdiv div selector selects any div within .outerdiv , not only direct children. .outerdiv div选择器选择任何 div.outerdiv ,不仅直接孩子。 To select a direct child of an element, there's the > selector (which is not supported by IE6 though). 要选择元素的直接子元素,请使用>选择器 (尽管IE6不支持)。

As for the last question, CSS rules are always reevaluated, ie even when manipulating the DOM, elements will always be styled according to the CSS rules and don't "carry over" rules through DOM manipulation or anything like that. 对于最后一个问题,始终会重新评估CSS规则,即,即使在操作DOM时,元素也将始终根据CSS规则进行样式设置,并且不会通过DOM操作或类似方法“继承”规则。

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

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