Mozilla Firefox 将伪类:-moz-ui-invalid
应用于任何无效的表单控件。如果表单从未提交,或者在调用表单的submit()
方法时所有表单输入控件都有效,那么调用表单的reset()
方法将导致从输入控件中删除伪类。
但是,如果在提交表单时任何表单的输入控件无效,那么即使在重置表单之后,伪类也将始终应用无效的输入控件。
如需完整文档和下载注释的 javascript 文件,请访问:http ://www.syn-to.com/moz-ui-invalid.php
var form;
(function()
{
"use strict";
//name must be a valid form name eg. <form name="myFormName" ...
form = function(name)
{
var a =
{
"registerReset": function()
{
//if the boxShadow property exists, bind the reset event handler
//to the named form
if(typeof document.forms[name].style.boxShadow !== 'undefined')
{
document.forms[name].addEventListener('reset', a.resetEventHandler);
}
},
"reset": function()
{
a.registerReset();
document.forms[name].reset();
},
"resetEventHandler": function()
{
//override the default style and apply no boxShadow and register
//an invalid event handler to each of the form's input controls
function applyFix(inputControls)
{
for(var i=0;i<inputControls.length;i++)
{
inputControls[i].style.boxShadow = 'none';
inputControls[i].addEventListener('invalid', a.invalidEventHandler);
inputControls[i].addEventListener('keydown', a.keydownEventHandler);
}
}
var inputControls = this.getElementsByTagName('input');
applyFix(inputControls);
var inputControls = this.getElementsByTagName('textarea');
applyFix(inputControls);
var inputControls = this.getElementsByTagName('select');
applyFix(inputControls);
},
"invalidEventHandler": function()
{
this.style.boxShadow = '';
this.removeEventListener('invalid', a.invalidEventHandler);
this.removeEventListener('keydown', a.keydownEventHandler);
},
//the following functions emulates the restore of :-moz-ui-invalid
//when the user interacts with a form input control
"keydownEventHandler": function()
{
this.addEventListener('blur', a.blurEventHandler);
},
"blurEventHandler": function()
{
this.checkValidity();
this.removeEventListener('blur', a.blurEventHandler);
}
};
return a;
}
})();
用法:
必须在本机重置(调用表单的reset()
方法)之前调用以下两种方法中的任何一种:
<form name="formName" ...>...</form>
form('formName').registerReset(); //registers the event handlers once
form('formName').reset(); //registers the event handlers at the time reset is called
//and then calls the form's native reset method, may be used
//in place of the native reset
在我看来,无论是否使用 ajax,也无论用户之前是否尝试提交带有无效输入控件的表单,重置都应该删除该样式!我已经提交了一个错误报告,但是到目前为止它不被认为是一个错误: https ://bugzilla.mozilla.org/show_bug.cgi?id=903200
这也应该帮助其他人。