跨瀏覽器的placeholder-jQuery版(jQuery外掛程式EnPlaceholder)

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   使用   2014   html   

案例:整搜尋方塊,需要預設預留位置為“請輸入關鍵詞”,擷取焦點時,預留位置消失或不可用(不影響正常輸入),丟失焦點後,若使用者無內容輸入,預留位置繼續出現,繼續佔位。這種代碼我想前端們已經很容易就寫出來了吧,現在HTML5原生就有這個“placeholder”屬性,效果與上邊案例描述的一樣(各支援的瀏覽器內部表現可能不一致,但是作用是一致的),那麼這一屬性該怎麼優雅降級到支援所有現代瀏覽器呢?答案還是指令碼即JavaScript。

上述案例的一個圖例:

 

 

下面我們就使用 Jquery 的 enplaceholder 外掛程式來實現這個效果.

效果示範介面 http://www.ifrans.cn/demo/enplaceholder.html

用法:

首先在head中分別引入jQuery及本外掛程式

<script type="text/javascript" src="jquery-1.7.2.min.js"></script>

<script type="text/javascript" src="jquery.enplaceholder.js"></script>

然後就可以調用鳥

//通過value類比placeholder

$(‘input‘).placeholder();

//通過插入span元素類比placeholder

$(‘input‘).placeholder({isUseSpan:true});

聯繫我們

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