请教一个JS的问题

来源:百度知道 编辑:UC知道 时间:2024/05/31 09:25:56
//让IE支持FOCUS
function styleform() {

var input = document.getElementsByTagName("input");
var textarea = document.getElementsByTagName("textarea");

for (var i = 0; i < input.length; i++)
{
input[i].onfocus = function() {
this.className = "focus";
}
input[i].onblur = function() {
this.className = "";
}
}

for (var j = 0; j < textarea.length; j++ )
{
textarea[j].onfocus = function() {
this.className = "focus";
}
textarea[j].onblur = function() {
this.className = "";
}
}
}

window.onload = function() {
if (document.all && !window.opera)
{
styleform();
}
}

//单击type="text"后选中

function focusFields () {
if (!document.getElementsByTagName) return false;
var formfields = document.getEleme

onfocus被付了两次值,后面的覆盖前面的。处理事件应该使用attachEvent("onfocus", function() {...})//IE和addEventListener("focus", function() {...})//Firefox

这段代码中还有问题,this.className="focus"和this.className=""为避免覆盖以前的class,应该用:
this.className+=" focus"和this.className=this.className.replace(/focus/ig, '');