跨瀏覽器placehoder

來源:互聯網
上載者:User

標籤:des   style   http   color   io   os   使用   ar   for   



<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>跨瀏覽器placehoder</title>
    <meta name="author" content="ifrans.cn" />
    <meta name="description" content="跨瀏覽器的placehoder原生js版,讓ie也支援placehoder" />
    <meta name="keywords" content="跨瀏覽器,placehoder,ie,原生js,表單" />

    <style>
        input{ vertical-align:middle; margin-left:10px; line-height:24px; width:200px; padding-left:2px; }
        textarea{ vertical-align:middle; margin-left:10px; width:200px; height:50px; font:inherit; padding-left:2px;}
.wrap-statistics{ visibility:hidden;}
    </style>
</head>
<body>
<form id="form1">
    <h3>在不支援placeholder的瀏覽器下,通過插入懸浮的span元素方式類比</h3>
    <p><label for="username1">使用者名稱:</label><input type="text" name="username1" id="username1" placeholder="請輸入使用者名稱" value="" required></p>
    <p><label for="password1">密 碼:</label><input type="password" name="password1" id="password1" placeholder="請輸入密碼" value="" required></p>
    <p><label for="address1">地 址:</label><input type="text" name="address1" id="address1" placeholder="請輸入地址" value="" required></p>
    <p><label for="remarks1">備 注:</label><textarea placeholder="請輸入備忘" id="remarks1"></textarea></p>
</form>
<form id="form2">
    <h3>通過value方式類比placeholder</h3>
    <p><label for="username2">使用者名稱:</label><input type="text" name="username1" id="username2" placeholder="請輸入使用者名稱" value="" required></p>
    <p><label for="address2">地 址:</label><input type="text" name="address1" id="address2" placeholder="請輸入地址" value="" required></p>
    <p><label for="remarks2">備 注:</label><textarea placeholder="請輸入備忘" id="remarks2"></textarea></p>
</form>
<script>
    var oForm1 = document.getElementById(‘form1‘);
    var oForm1Inputs = oForm1.getElementsByTagName(‘input‘);
    for(var i=0;i<oForm1Inputs.length;i++){
        placeHolder(oForm1Inputs[i],true);
    }
    var oForm1Textarea = oForm1.getElementsByTagName(‘textarea‘)[0];
    placeHolder(oForm1Textarea,true);


    var oForm2 = document.getElementById(‘form2‘);
    var oForm2Inputs = oForm2.getElementsByTagName(‘input‘);
    for(var i=0;i<oForm2Inputs.length;i++){
        placeHolder(oForm2Inputs[i]);
    }
    var oForm2Textarea = oForm2.getElementsByTagName(‘textarea‘)[0];
    placeHolder(oForm2Textarea);


    /**
     * @name placeHolder
     * @class 跨瀏覽器placeHolder,對於不支援原生placeHolder的瀏覽器,通過value或插入span元素兩種方案類比
     * @param {Object} obj 要應用placeHolder的表單元素對象
     * @param {Boolean} span 是否採用懸浮的span元素方式來類比placeHolder,預設值false,預設使用value方式類比
     */
    function placeHolder(obj, span) {
        if (!obj.getAttribute(‘placeholder‘)) return;
        var imitateMode = span===true?true:false;
        var supportPlaceholder = ‘placeholder‘ in document.createElement(‘input‘);
        if (!supportPlaceholder) {
            var defaultValue = obj.getAttribute(‘placeholder‘);
            if (!imitateMode) {
                obj.onfocus = function () {
                    (obj.value == defaultValue) && (obj.value = ‘‘);
                    obj.style.color = ‘‘;
                }
                obj.onblur = function () {
                    if (obj.value == defaultValue) {
                        obj.style.color = ‘‘;
                    } else if (obj.value == ‘‘) {
                        obj.value = defaultValue;
                        obj.style.color = ‘#ACA899‘;
                    }
                }
                obj.onblur();
            } else {
                var placeHolderCont = document.createTextNode(defaultValue);
                var oWrapper = document.createElement(‘span‘);
                oWrapper.style.cssText = ‘position:absolute; color:#ACA899; display:inline-block; overflow:hidden;‘;
                oWrapper.className = ‘wrap-placeholder‘;
                oWrapper.style.fontFamily = getStyle(obj, ‘fontFamily‘);
                oWrapper.style.fontSize = getStyle(obj, ‘fontSize‘);
                oWrapper.style.marginLeft = parseInt(getStyle(obj, ‘marginLeft‘)) ? parseInt(getStyle(obj, ‘marginLeft‘)) + 3 + ‘px‘ : 3 + ‘px‘;
                oWrapper.style.marginTop = parseInt(getStyle(obj, ‘marginTop‘)) ? getStyle(obj, ‘marginTop‘): 1 + ‘px‘;
                oWrapper.style.paddingLeft = getStyle(obj, ‘paddingLeft‘);
                oWrapper.style.width = obj.offsetWidth - parseInt(getStyle(obj, ‘marginLeft‘)) + ‘px‘;
                oWrapper.style.height = obj.offsetHeight + ‘px‘;
                oWrapper.style.lineHeight = obj.nodeName.toLowerCase()==‘textarea‘? ‘‘:obj.offsetHeight + ‘px‘;
                oWrapper.appendChild(placeHolderCont);
                obj.parentNode.insertBefore(oWrapper, obj);
                oWrapper.onclick = function () {
                    obj.focus();
                }
//綁定input或onpropertychange事件
                if (typeof(obj.oninput)==‘object‘) {
                    obj.addEventListener("input", changeHandler, false);
                } else {
                    obj.onpropertychange = changeHandler;
                }
                function changeHandler() {
                    oWrapper.style.display = obj.value != ‘‘ ? ‘none‘ : ‘inline-block‘;
                }
                /**
                 * @name getStyle
                 * @class 擷取樣式
                 * @param {Object} obj 要擷取樣式的對象
                 * @param {String} styleName 要擷取的樣式名
                 */
                function getStyle(obj, styleName) {
                    var oStyle = null;
                    if (obj.currentStyle)
                        oStyle = obj.currentStyle[styleName];
                    else if (window.getComputedStyle)
                        oStyle = window.getComputedStyle(obj, null)[styleName];
                    return oStyle;
                }
            }
        }
    }
</script>




</body>
</html>

跨瀏覽器placehoder

聯繫我們

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