當前位置: 首頁IT技術(shù) → 文本框獲得失去焦點——js和jquery方法的對比

文本框獲得失去焦點——js和jquery方法的對比

更多
Js方法
<head>
    <script type="text/javascript">
        function Doit() {
            // 獲得一個input的數(shù)組,需要遍歷
            var inputs = document.getElementsByTagName_r("input");           
              for (var i = 0; i < inputs.length; i++) {
              
                // 如果是文本控件
                if (inputs[i].type == "text") {
                    // 前面有on——
                    inputs[i].onfocus = function () {
                        // 通過this直接獲取觸發(fā)的元素
                        this.style.backgroundColor = "yellow";
                    };
                    inputs[i].onblur = function () {
                        this.style.backgroundColor = "white";
                    };
                }
             
            }
        }
    </script>
</head>
<body onload="Doit()">
    <input id="Text1" type="text" />
    <input id="Text2" type="text" />
    <input id="Text3" type="text" />
    <input id="Button1" type="button" value="button" />
</body>
</html>
 
Jquery方法
<head>
    <script src="jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            // 不需要遍歷
            $('input[type=text]').click(function () {
                // this前面要加dollar符
                $(this).css("backgroundColor","yellow");
            }).blur(function () { $(this).css("backgroundColor","white");});
            // blur前面沒有on
        });
    </script>
</head>
<body>
    <input id="Text1" type="text" />
    <input id="Text2" type="text" />
    <input id="Text3" type="text" />
    <input id="Button1" type="button" value="button" />
</body>
</html>
熱門評論
最新評論
發(fā)表評論 查看所有評論(0)
昵稱:
表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
字數(shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)