版本一
css代碼部分:
復(fù)制代碼 代碼如下:
.focus {
border: 1px solid #f00;
background: #fcc;
}
html代碼部分:
復(fù)制代碼 代碼如下:
<body>
<form action="" method="post">
<fieldset>
<legend>個人基本信息</legend>
<div>
<label for="username">名稱:</label>
<input type="text" />
</div>
<div>
<label for="pass">密碼:</label>
<input type="password" />
</div>
<div>
<label for="msg">詳細信息:</label>
<textarea rows="2" cols="20"></textarea>
</div>
</fieldset>
</form>
</body>
:input匹配 所有 input, textarea, select 和 button 元素。
jQuery代碼部分:
復(fù)制代碼 代碼如下:
<script type="text/javascript">
$(function(){
$(":input").focus(function(){
$(this).addClass("focus");
}).blur(function(){
$(this).removeClass("focus");
});
})
</script>
版本二:
有時候文本框里有默認的內(nèi)容,作為提示信息,獲取焦點后,要讓它消失。可以做如下的改造:
復(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>
失去焦點,如果文本框中為空,也就是沒有輸入內(nèi)容,就將值還設(shè)為默認值。
這是一個簡單的邏輯。
新聞熱點
疑難解答
圖片精選