如何选择作为给定元素的子元素的多个元素?

2022-01-10 00:00:00 css-selectors css

我有 <div id='mydiv'> 我需要选择所有 prediv 的子元素#mydiv.

I have <div id='mydiv'> and I need to select all pre and div elements that are children of #mydiv.

我可以这样做:

div#mydiv > pre, div#mydiv > div

但是,是否可以做到只引用一次#mydiv?

but, can it be done so that #mydiv is referenced only once?

div#mydiv > pre, div

将选择页面上的所有 div,无论它们是否是 #mydiv 的子级,因此逗号不是一种方法.也许还有另一种我不知道的语法?

will select all divs on the page regardless if they're children of #mydiv, so the comma isn't a way to do it. Maybe there's another kind of syntax I don't know about?

推荐答案

你必须引用 #mydiv 两次...

You'll have to reference #mydiv twice...

#mydiv > pre, #mydiv > div

我删除了无关的 div 元素选择器,因为 ID 足够具体.

I removed the extraneous div element selector as the ID is specific enough.

相关文章