javascript中onfocus,onblur事件提示不同文字效果

來源:互聯網
上載者:User

例子

表單中滑鼠移進字型消失,移出提示字型顯示

 代碼如下 複製代碼

//捕獲焦點時
function onFocusFun(element, elementvalue) {
if (element.value == elementvalue) {
element.value = “”;
}
}
//離開輸入框觸發事件
function onBlurFun(element, elementvalue) {
if (element.value == “” || element.value.replace(/\s/g, “”) == “”) { //滑鼠移出後不管文字框中的值是否為空白都替換為空白
element.value = elementvalue; //將值賦予文字框
}
}
用法:<input type=”text” class=”"  value=”" id=”username” name=”username” onfocus=”onFocusFun(this, ‘使用者名稱/手機號’)” onblur=”onBlurFun(this, ‘使用者名稱/手機號’)”  />

現在這個年代對於網頁最佳化都非常的重要了,我們可以使用jquery來實現並且jquery代碼更簡潔。

 代碼如下 複製代碼

<input id="xf_search" type="text" />
<script type="text/javascript">
(function (){ $("#xf_search").focus(function (){(this.value=='請輸入樓盤名稱')?this.value='':false}); $("#xf_search").blur(function (){(this.value=='')?this.value='請輸入樓盤名稱':false}); }());
</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.