如何允许表单元素具有多个类,并为错误消息连接/切片
How to allow form elements to have more than one class and concatenate / slice for error message
我正试图通过允许表单元素的标签来更改此代码有不止一节课。我知道我必须使用串联来添加错误类,并通过剪切最后一个空格后的文本来删除错误类在className值中,但我从哪里开始,我应该只修改跨度吗?
function addErrorMessage(id, msg) {
'use strict';
// Get the form element reference:
var elem = document.getElementById(id);
// Define the new span's ID value:
var newId = id + 'Error';
// Check for the existence of the span:
var span = document.getElementById(newId);
if (span) {
span.firstChild.value = msg; // Update
} else { // Create new.
// Create the span:
span = document.createElement('span');
span.id = newId;
span.className = 'error'
span.appendChild(document.createTextNode(msg));
// Add the span to the parent:
elem.parentNode.appendChild(span);
elem.previousSibling.className = 'error';
} // End of main IF-ELSE.
} // End of addErrorMessage() function.
// This function removes the error message.
// It takes one argument: the form element ID.
function removeErrorMessage(id) {
'use strict';
// Get a reference to the span:
var span = document.getElementById(id + 'Error');
if (span) {
// Remove the class from the label:
span.previousSibling.previousSibling.className = null;
// Remove the span:
span.parentNode.removeChild(span);
} // End of IF.
} // End of removeErrorMessage() function.
为什么不使用jQuery,这样您就可以简单地使用addClass和removeClass?
http://api.jquery.com/addclass/
http://api.jquery.com/removeclass/
你还可以从hasClass之类的东西中获益。。。http://api.jquery.com/hasclass/
相关文章:
- jQuery在输入下验证post错误消息
- 在AngularJs中隐藏默认错误消息
- 如果用户输入的长度小于最小长度,则显示错误消息
- 使用ASP.NET CustomValidator避免重复的错误消息
- KOValidation在错误消息中获取可观察值、$index()、$data等
- 更改精细上载中的错误消息
- 如何在页面刷新时隐藏错误消息
- 如何显示错误消息
- 滑块未显示,控制台中没有错误消息
- 表单提交没有'如果为空,则不会显示错误消息
- 为什么我的Alexa技能测试显示正确的lambda输出,但在开发人员控制台中测试时却给出错误消息
- 隐藏错误消息“;未选择文件”;asp:fileUpload的文本
- 登录时显示自定义错误消息
- 如何仅在存在最小值和最大值时才显示错误消息
- 通过 AJAX 检索 Blob 时处理错误消息
- Javascript 错误:消息:预期的“)”
- 如何将错误消息从 meteor 服务器传递到客户端
- 为什么 Angularjs 总是显示错误消息
- 我如何让我的 var 错误消息输出是使用字段还是字段,具体取决于留空的字段数
- 将错误消息 JSON 传递到另一个页面模板