Javascript–如何?浮水印效果

來源:互聯網
上載者:User

:

如何?圖中效果:

1.在文字框中給出提示

2.當點擊輸入時,提示消失

3.當點擊文字框,但未輸入資料,失去焦點時仍給出提示.

實現過程如下:

1.一個span和一個文字框

2.設定span的常值內容為提示內容

3.設定文字框的的樣式和事件.

主要就是設定文字框的位置,設定絕對位置,定位在span的上面,然後設定文字框的背景顏色為transparent即可.

具體代碼如下:

<html xmlns="http://www.w3.org/1999/xhtml"><head>    <title>無標題頁</title>    <style type="text/css">                        #txt        {        position:absolute;        top:10px;        left:10px;         background-color:Transparent;         z-index:100;                  }                #txt2        {        position:absolute;        top:35px;        left:10px;        background-color:Transparent;        }                .span1        {        color:Silver;        left:10px;        }                .span2        {        color:White;        left:10px;        }    </style>    </head><body style="margin:10px">    <span class="span1" id='sp'>        姓名:    </span>    <input type="text" id='txt' />    <br />    <span class="span1" id='sp2'>        密碼:    </span>    <input type="text" id='txt2' />    <script type="text/javascript">        document.getElementById("txt").onclick=function(){            document.getElementById("sp").className="span2";        };                document.getElementById("txt2").onclick=function(){            document.getElementById("sp2").className="span2";        };                document.getElementById("txt").onblur=function(){            if(window.event.srcElement.value==''){                document.getElementById("sp").className="span1";            }        };                document.getElementById("txt2").onblur=function(){            if(window.event.srcElement.value==''){                document.getElementById("sp2").className="span1";            }        };            </script></body></html>
相關文章

聯繫我們

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