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

问题描述:

我有<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

由于ID足够具体,我删除了多余的div元素选择器.

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