﻿/* 
动态设置输入文本控件的 focus和blur事件 
引用此文件后 在页面加上 .txtFocus 和.txtBlur的样式即可
*/

var input = document.getElementsByTagName("input");
for (j=0 ;j<input.length; j++)
{
    var str = input.item(j).attributes.getNamedItem("type").nodeValue;
    if (str == "text" || str == "password")
    {
        var tb = input.item(j);
        tb.attributes.getNamedItem("class").nodeValue = "txtBlur";
        seting(tb);
    }
}

function seting(obj)
{
    if(window.addEventListener)
    {   // Mozilla, Netscape, Firefox
        obj.addEventListener("focus", show(obj,1), true);
        obj.addEventListener("blur", show(obj,0), true);
    }
    else
    {   // IE
        obj.attachEvent("onfocus",  function(){show(obj,1);});
        obj.attachEvent("onblur",  function(){show(obj,0);});
    }
}

function show(obj,n)
{
    if (n == 1)
    {
        obj.className = "txtFocus";
    }
    else
    {
        obj.className = "txtBlur";
    }
}