仅在 jQuery 验证插件中为错误消息的元素自定义 class

Customizing class for element of error message only in jQuery Validation plugin

在 jQuery Validate 插件中,设置 errorClass 会同时设置错误消息和输入元素的 class。无论如何只为错误消息元素设置 class?

不,插件会动态地将 errorClass 添加到输入和错误消息元素中 并且您无法更改此行为,您也不需要更改它.毕竟,在您创建一些针对它们的 CSS 属性之前,在元素上设置 类 没有任何作用。

因此,通过使用正确的 CSS 选择器,您可以轻松地分别定位这些元素。

验证消息的默认 label 容器以及默认 errorClass 设置,即 "error"...

label.error {
    /* CSS properties targeting only the error messages */
}

select.error,
textarea.error,
input[type="text"].error,
input[type="radio"].error,
input[type="checkbox"].error {
    /* CSS properties targeting only the input elements */
}

演示:http://jsfiddle.net/j3te0d10/


Is there anyway to set class for error message element only?

您永远不需要这样做。

只需在 CSS 中定位 label.error,只有错误消息会受到影响。

label.error {
    /* CSS properties targeting only the error messages */
}

演示 2:http://jsfiddle.net/j3te0d10/1/