相容placeholder,placeholder相容性

來源:互聯網
上載者:User

相容placeholder,placeholder相容性

眾所周知,IE9以下不相容placeholder屬性,因此自訂了一個jQuery外掛程式placeHolder.js。下面為placeHolder.js的代碼:

/** * 該控制項相容IE9以下,專門針對IE9以下不支援placeholder屬性所做 * Author: quranjie * Date:2014-09-26 */$(function() {// 如果不支援placeholder,用jQuery來完成if(!isSupportPlaceholder()) {// 遍曆所有input對象, 除了密碼框$('input').not("input[type='password']").each(function() {var self = $(this);var val = self.attr("placeholder");input(self, val);});/* 對password框的特殊處理 * 1.建立一個text框  * 2.擷取焦點和失去焦點的時候切換 */$('input[type="password"]').each(function() {var pwdField    = $(this);  var pwdVal      = pwdField.attr('placeholder');  var pwdId       = pwdField.attr('id');  // 重新命名該input的id為原id後跟1pwdField.after('<input id="' + pwdId +'1" type="text" value='+pwdVal+' autocomplete="off" />');  var pwdPlaceholder = $('#' + pwdId + '1');  pwdPlaceholder.show();  pwdField.hide();    pwdPlaceholder.focus(function(){  pwdPlaceholder.hide();  pwdField.show();  pwdField.focus();  });    pwdField.blur(function(){  if(pwdField.val() == '') {  pwdPlaceholder.show();  pwdField.hide();  }  });  });}});// 判斷瀏覽器是否支援placeholder屬性function isSupportPlaceholder() {var input = document.createElement('input');return 'placeholder' in input;}// jQuery替換placeholder的處理function input(obj, val) {var $input = obj;var val = val;$input.attr({value:val});$input.focus(function() {if ($input.val() == val) {$(this).attr({value:""});}}).blur(function() {if ($input.val() == "") {            $(this).attr({value:val});}});}

調用方法:

<html><head><title>替換placeholder屬性 相容IE demo</title><style type="text/css">input {height: 20px;width: 150px;}</style><script type="text/javascript" src="js/jquery-1.7.2.min.js" ></script><script type="text/javascript" src="js/placeHolder.js" ></script><script type="text/javascript">function check(){// 列印出password的值var passValue = $('#password').val();alert(passValue);return false;};</script></head><body><form action="#"><input id="username" type="text" placeholder="請輸入使用者名稱" /><input id="password" type="password" placeholder="請輸入密碼" /><input id="confirm" type="password" placeholder="請確認密碼" /><br /><br /><input type="submit" onclick="return check();"/></form></body></html>
本代碼相容IE9以下,jQuery選擇1.7.2的穩定版本,代碼實現考慮到一個頁面有多個password輸入框的情況。


插播廣告,本人想在工作之餘做些兼職項目掙點外快,項目領域主要為:Android、IOS、PHP以及網站整站建設,其中尤其擅長Android、PHP和網站建設。

有意者請私信給我!非誠勿擾,謝謝!


有沒有啥外掛程式讓ie6 ie7 ie8 支援 input 中的 placeholder 屬性?

placeholder 是HTML5的。
在IE6-8中不能使用。可以使用 JQ 來設定。
給input一個value, JQ判斷得到焦點時 value="";
移開焦點時,若input為空白,那麼再加上"請輸入即可"

具體例子如下:
$(document).ready(function() { $("input").focusin(function() { if($(this).val() =="請輸入"){ $(this).val(""); } }); $("input").focusout(function() { if($(this).val() ==""){ $(this).val("請輸入"); } });});
 
placeholder是什

幫你找到的
一個變數,在這裡就代表新開啟的那個視窗。
那就舉個例子吧
<html>
<script langruage="javascript">
var placeHolder = window.open('holder.html','placeholder','width=200,height=200');

</script>
</html>
把上面的程式存為一個.html檔案,點擊開啟的時候,會彈出一個新的視窗。那個新的視窗在程式裡邊就可以用placeHolder來控制。接著再看這個:
<html>
<script langruage="javascript">
var placeHolder = window.open('holder.html','placeholder','width=200,height=200');
placeHolder.close();
</script>
</html>
加了一句話:placeHolder.close();運行新的程式,快顯視窗剛開啟,馬上就會被關閉,這樣不知道你清楚了嗎?
 

聯繫我們

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