简体   繁体   English

父类中多个ID的CSS选择器

[英]CSS selector for several id's within a parent class

I am looking for a way to target several different id's within a parent class with a css selector. 我正在寻找一种方法来针对具有CSS选择器的父类中的多个不同ID。

I am aware that its not common to do this since id's need to be unique in order to validate, and thus there is limited practical use for such a solution. 我知道这样做并不普遍,因为id需要唯一才能进行验证,因此这种解决方案的实际用途有限。 I was initially intending to use this code for a school project. 我最初打算将此代码用于学校项目。 I've since found a valid workaround but I still wonder if this can be done, and if so how. 从那以后,我找到了一个有效的解决方法,但我仍然想知道这是否可以实现,以及如何实现。

           <form class="example6">
                <div>
                    <h3>Contact form</h3>
                    <label>
                        <span>Your name</span><input  id="name" type="text" name="name" />
                    </label>

                    <label>
                        <span>Email Address</span><input id="email" type="text" name="email" />
                    </label>

                    <label>
                        <span>Subject</span><input  id="subject" type="text" name="subject" />
                    </label>

                    <label>
                        <span>Message</span><textarea  name="feedback"></textarea>

                    </label>
                 </div>
             </form>

I want to select #name, #email and #subject, but only if within .example6. 我想选择#name,#email和#subject,但前提是在.example6中。

A descendant selector is a way to select only children of the ancestor / parent ( parent child ) whether they are elements, classes, IDs etc. The selection is denoted by the space between selectors 后代选择器是一种仅选择祖先/父代( parent child )的parent child (无论它们是元素,类,ID等)的方式。选择器之间用空格表示

.example6 #name {

}

You can comma separate for multiple selectors and apply the same CSS 您可以逗号分隔多个选择器并应用相同的CSS

.example6 #name, .example6 #email, .example6 #subject {

}

jsFiddle Demo jsFiddle演示

You can use this: 您可以使用此:

.example6 > div > label > input{
    border-color: red;
}

fiddle 小提琴

Try this: 尝试这个:

.example6 #name, .example6 #email, .example6 #subject {
    ...
}

There are several ways to do this. 有几种方法可以做到这一点。

You can comma separate multiple expresions: 您可以用逗号分隔多个表达式:

.example6 #name, .example6 #email, .example6 #subject {...}

or find something else that's unique to the element (eg: they are the only inputs within .example6 ) 或找到该元素唯一的其他内容(例如:它们是.example6中的唯一inputs

.example6 input{...}

or use attribute selectors to get just the inputs having type=text: 或使用属性选择器仅获取具有type = text的输入:

.example6 input[type=text]{...}

http://jsfiddle.net/au0drh1w/ http://jsfiddle.net/au0drh1w/

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

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