使用css3和jquery.validate使必填字段显示为红色

时间:2021-03-16 11:50:08

I'm trying to make required fields in a form to appear red, but am not have any luck. Below is my form and the css I have tried.

我试图让表格中的必填字段显示为红色,但我没有运气。下面是我的表格和我尝试过的CSS。

contact.html

                        <h2>Send us an email...</h2>
                        <br/>
                        <ul>

                            <li>
                                <label for="senderName"  >Your Name</label>
                                <input type="text" name="senderName" id="senderName" required="required" placeholder="Please type your name"   maxlength="40" />
                            </li>

                            <li>
                                <label for="senderEmail" >Your Email Address</label>
                                <input type="email" name="senderEmail" id="senderEmail" required="required"  placeholder="Please type your email address"   maxlength="50" />
                            </li>

                            <li>
                                <label for="message" style="padding-top: .5em;" >Your Message</label>
                                <textarea name="message" id="message" placeholder="Please type your message" required="required" cols="80" rows="10" maxlength="10000"></textarea>
                            </li>

                        </ul>
                        <div id="formButtons">
                            <input type="submit" id="sendMessage" name="sendMessage" value="Send Email" />
                            <input type="button" id="cancel" name="cancel" value="Cancel" />
                        </div>
                    </form>

style.css

label[required=required]{
    color: darkred;
}

label[] > input[required="required"] {
    background: red;  }

1 个解决方案

#1


3  

Since the <input> element has the required attribute and appears after the <label>, you would want an previous sibling selector, which unfortunately does not exist in CSS. You cannot select backwards in CSS, only forwards.

由于元素具有必需属性并出现在

You could use the + selector but it would require re-arranging your HTML so that the <label> is after the <input>, Once you do this, this would work:

你可以使用+选择器,但它需要重新安排你的HTML,以便

input[required]+label {
    background: red;
}

If this is not desirable, you would need to give the label a class of its own which you can use, like <label class="required">...</label>.

如果这不合适,您需要为标签指定一个您可以使用的类,例如

See this related question: Opposite of adjacent sibling selector?

看到这个相关的问题:相邻的兄弟选择器的对面?

#1


3  

Since the <input> element has the required attribute and appears after the <label>, you would want an previous sibling selector, which unfortunately does not exist in CSS. You cannot select backwards in CSS, only forwards.

由于元素具有必需属性并出现在

You could use the + selector but it would require re-arranging your HTML so that the <label> is after the <input>, Once you do this, this would work:

你可以使用+选择器,但它需要重新安排你的HTML,以便

input[required]+label {
    background: red;
}

If this is not desirable, you would need to give the label a class of its own which you can use, like <label class="required">...</label>.

如果这不合适,您需要为标签指定一个您可以使用的类,例如

See this related question: Opposite of adjacent sibling selector?

看到这个相关的问题:相邻的兄弟选择器的对面?