欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery 文本框得失焦點(diǎn)的簡(jiǎn)單實(shí)例

 更新時(shí)間:2014年02月19日 09:40:40   作者:  
本篇文章主要是對(duì)jQuery 文本框得失焦點(diǎn)的簡(jiǎn)單實(shí)例進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助

版本一

css代碼部分:

復(fù)制代碼 代碼如下:

.focus {
     border: 1px solid #f00;
     background: #fcc;
}

當(dāng)焦點(diǎn)獲得時(shí),添加focus樣式,添加邊框,并改背景色為#fcc

html代碼部分:

復(fù)制代碼 代碼如下:

<body>
    <form action="" method="post" id="regForm">
        <fieldset>
            <legend>個(gè)人基本信息</legend>
                <div>
                    <label  for="username">名稱(chēng):</label>
                    <input id="username" type="text" />
                </div>
                <div>
                    <label for="pass">密碼:</label>
                    <input id="pass" type="password" />
                </div>
                <div>
                    <label for="msg">詳細(xì)信息:</label>
                    <textarea id="msg" rows="2" cols="20"></textarea>
                </div>
        </fieldset>
    </form>
</body>

這里有兩個(gè)input,一個(gè)textare框。

:input匹配 所有 input, textarea, select 和 button 元素。

jQuery代碼部分:

復(fù)制代碼 代碼如下:

<script type="text/javascript">
    $(function(){
        $(":input").focus(function(){
              $(this).addClass("focus");
        }).blur(function(){
              $(this).removeClass("focus");
        });
    })
    </script>

用:input匹配所有的input元素,當(dāng)獲取焦點(diǎn)時(shí),就添加樣式focus,通過(guò)$(this)自動(dòng)識(shí)別當(dāng)前的元素。focus()方法是獲取焦點(diǎn)事件發(fā)生時(shí)執(zhí)行的函數(shù)。blur()方法是失去焦點(diǎn)事件發(fā)生時(shí)執(zhí)行的函數(shù)。

版本二:

有時(shí)候文本框里有默認(rèn)的內(nèi)容,作為提示信息,獲取焦點(diǎn)后,要讓它消失??梢宰鋈缦碌母脑欤?BR>

復(fù)制代碼 代碼如下:

<script type="text/javascript">
    $(function(){
        $(":input").focus(function(){
              $(this).addClass("focus");
              if($(this).val() ==this.defaultValue){ 
                  $(this).val("");          
              }
        }).blur(function(){
             $(this).removeClass("focus");
             if ($(this).val() == '') {
                $(this).val(this.defaultValue);
             }
        });
    })
    </script>

做個(gè)邏輯判斷,如果值為默認(rèn)值,就將文本框中的內(nèi)容清空。

失去焦點(diǎn),如果文本框中為空,也就是沒(méi)有輸入內(nèi)容,就將值還設(shè)為默認(rèn)值。

這是一個(gè)簡(jiǎn)單的邏輯。

相關(guān)文章

最新評(píng)論