阅读508 返回首页    go 阿里云 go 技术社区[云栖]


文本框获得失去焦点——js和jquery方法的对比

 

Js方法

<head>
    <script type="text/javascript">
        function Doit() {

            // 获得一个input的数组,需要遍历
           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直接获取触发的元素
                       this.style.backgroundColor = "yellow";
                    };
                    inputs[i].onblur = function () {
                        this.style.backgroundColor = "white";
                    };
                }
              
            }
        }
    </script>
</head>
<body >
    <input type="text" />
    <input type="text" />
    <input type="text" />
    <input 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 type="text" />
    <input type="text" />
    <input type="text" />
    <input type="button" value="button" />
</body>
</html>

最后更新:2017-04-02 22:16:01

  上一篇:go Struts2中操作request,session,application的方法
  下一篇:go Js控制css——模仿视频网站关灯开灯