jS實現文字框在點擊時變色

來源:互聯網
上載者:User

標籤:web前端   ui   jquery   前端   web伺服器   

網頁上預設的文字框老是灰色風格,看的都有點不耐煩了,用CSS和JS改變其樣式是大家都喜歡用的方法,今天寫了一個點擊邊框變色的文字框,滑鼠點擊文字框將要輸入的時候,文字框自動變色高亮顯示,非常有視覺效果,讓文字框變漂亮了許多。HTML代碼如下:

.代碼  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="textml; charset=utf-8" />  
  5. <title>輸入框點擊時邊框變色效果</title>  
  6. </head>  
  7. <body>  
  8. <script type="text/javascript">   
  9. // focusClass : 擷取焦點時的樣式  
  10. // normalClass : 正常狀態下的樣式  
  11. function focusInput(focusClass, normalClass) {  
  12.     var elements = document.getElementsByTagName("input");  
  13.     for (var i=0; i < elements.length; i++) {  
  14.         if (elements[i].type != "button" && elements[i].type != "submit" && elements[i].type != "reset") {  
  15.             elements[i].onfocus = function() { this.className = focusClass; };  
  16.             elements[i].onblur = function() { this.className = normalClass||‘‘; };  
  17.         }  
  18.     }  
  19. }  
  20. </script>  
  21. <script type="text/javascript">  
  22. window.onload = function () {  
  23.     focusInput(‘focusInput‘, ‘normalInput‘);  
  24. }  
  25. </script>  
  26. 請輸入姓名:<input type="text" class="normalInput" />  
  27. <style type="text/css">  
  28. .normalInput {border:1px solid #ccc;}  
  29. .focusInput {border:1px solid #FFD42C;}  
  30. </style>  
  31. </body>  
  32. </html>  

 

 

在Firefox下也有效,只不過Firefox和chrome下,這兩款瀏覽器預設會自動會輸入框添加點擊效果,所以有時候看不清,IE下表現突出。前端資源分享

聯繫我們

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