JavaScript如何?在文字框(密碼框)輸入提示_javascript技巧

來源:互聯網
上載者:User

有時候我們需要在登陸表單有一些提示言,比如“請輸入使用者名稱”和“請輸入密碼”等語言,至於使用者名稱好說,但是在密碼框中出現“請輸入密碼”這樣的語言就有點麻煩了,因為在密碼框輸入的內容不會以明碼顯示。如果動態控制type屬性的話就有相容性問題,如果input已經存在於頁面中的話,在IE8和IE8以下瀏覽器中,type屬性是唯讀。所以就得想其他辦法,代碼如下:

<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/" /><title>雲棲社區</title><style type="text/css">#tx{width:100px;}#pwd{display:none;width:100px;}</style><script type="text/javascript"> window.onload=function(){var tx=document.getElementById("tx");var pwd=document.getElementById("pwd"); tx.onfocus=function(){ if(this.value!="密碼") return; this.style.display="none"; pwd.style.display="block"; pwd.value=""; pwd.focus(); } pwd.onblur=function(){ if(this.value!=""){return; } this.style.display="none"; tx.style.display=""; tx.value="密碼"; } }</script> </head> <body> <input type="text" value="密碼" id="tx"/> <input type="password" id="pwd" /> </body> </html> 

以上代碼實現了我們的要求,可以出現明碼的提示,當輸入密碼的時候就是以密碼方式輸入。

實現的原理非常的簡單,在預設狀態以type="text"文字框顯示,當點擊文字框的時候,以type="password"密碼框顯示,原來顯示的文字框隱藏,也就是說做了一個替換而已。

以上內容是小編給大家分享的JavaScript如何?在密碼框中出現提示的相關知識,希望大家喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.