html5的placeholder屬性(IE如何相容placeholder屬性)

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   strong   

介面UI推薦jquery html5 實現placeholder相容password  IE6html5的placeholder屬性(IE如何相容placeholder屬性) 

2013-01-05 10:26:06|  分類: web學習 |  標籤:html  js  ie  placeholder  |舉報 |字型大小大中小 訂閱

  

placeholder屬性,IE對其的支援表示無奈,firefox、google chrome表示毫無壓力。

 

HTML5對Web Form做了許多增強,比如input新增的type類型、Form Validation等。Placeholder是HTML5新增的另一個屬性,當input或者textarea設定了該屬性後,該值的內容將作為灰字提示顯示在文字框中,當文字框獲得焦點時,提示文字消失。以前要實現這效果都是用JavaScript來控制才能實現:

<input id="t1" type="text" placeholder="請輸入文字" />

由於placeholder是個新增屬性,目前只有少數瀏覽器支援,如何檢測瀏覽器是否支援它呢?(更多HTML5/CSS3特性檢測可以訪問)

 

預設提示文字是灰色的,可以通過CSS來改變文字樣式:

 

相容其他不支援placeholder的瀏覽器:

 

介紹一個超強的讓IE下支援placeholder的屬性外掛程式,代碼如下:

 

 $(document).ready(function () {
            var doc = document,
                inputs = doc.getElementsByTagName(‘input‘),
                supportPlaceholder = ‘placeholder‘ in doc.createElement(‘input‘),
                placeholder = function (input) {
                    var text = input.getAttribute(‘placeholder‘),
                        defaultValue = input.defaultValue;
                    if (defaultValue == ‘‘) {
                        input.value = text
                    }
                    input.onfocus = function () {
                        if (input.value === text) {
                            this.value = ‘‘
                        }
                    };
                    input.onblur = function () {
                        if (input.value === ‘‘) {
                            this.value = text
                        }
                    }
                };
            if (!supportPlaceholder) {
                for (var i = 0, len = inputs.length; i < len; i++) {
                    var input = inputs[i],
                        text = input.getAttribute(‘placeholder‘);
                    if (input.type === ‘text‘ && text) {
                        placeholder(input)
                    }
                }
            }
        });

 

直接把代碼複製下來,儲存成一個js檔案引用即可,根本不用再做任何處理,超級方便~

 此文源於http://lidrema.blog.163.com/blog/static/209702148201305101844932/

聯繫我們

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