簡體   English   中英

如果輸入類型為密碼,則占位符不適用於Internet Explorer,但仍顯示*********

[英]placeholder-not-working-for-internet-explorer if input type is password, still its showing the *********

我在占位符上得到1個答案, 不適用於Internet Explorer

我正在使用此代碼,

window.onload = function() { 
    var arrInputs = document.getElementsByTagName("input"); 
    for (var i = 0; i < arrInputs.length; i++) { 
        var curInput = arrInputs[i]; 
        if (!curInput.type || curInput.type == "" || curInput.type == "text"|||| curInput.type == "password") 
            HandlePlaceholder(curInput); 
    } 
}; 



function HandlePlaceholder(oTextbox) { 
    if (typeof oTextbox.placeholder == "undefined") { 
        var curPlaceholder = oTextbox.getAttribute("placeholder"); 
        if (curPlaceholder && curPlaceholder.length > 0) { 
            oTextbox.value = curPlaceholder; 
            oTextbox.setAttribute("old_color", oTextbox.style.color); 
            oTextbox.style.color = "#c0c0c0"; 
            oTextbox.onfocus = function() { 
                this.style.color = this.getAttribute("old_color"); 
                if (this.value === curPlaceholder) 
                    this.value = ""; 
            }; 
            oTextbox.onblur = function() { 
                if (this.value === "") { 
                    this.style.color = "#c0c0c0"; 
                    this.value = curPlaceholder; 
                } 
            }; 
        } 
    } 
} 

太好了,但是現在我遇到了一個問題,代替顯示“密碼”,而是顯示********特殊符號,有什么辦法可以解決這個問題

解決該問題的唯一方法是使用另一個元素(隱藏原始元素,或在其上放置新元素)。 不幸的是,Internet Explorer不允許您更改input元素的type屬性。 此代碼(來自我的placeholder polyfill, Placeholders.js )演示了該問題:

if (element.type === "password") {
    // The `type` property is read-only in IE < 9, so in those cases we just move on. The placeholder will be displayed masked
    try {
        element.type = "text";
        element.setAttribute("data-placeholdertype", "password");
    } catch (e) {}
}

這意味着我們可以很好地支持除IE之外的所有瀏覽器。 除非您要創建一個新元素(這不是我要在polyfill中采用的路線),否則恐怕您將不得不忍受它。

IE 9及更低版本不支持占位符參數: http : //caniuse.com/#feat=input-placeholder

(我想這就是你寫自己的東西的原因)

唯一的選擇是在顯示占位符時將type="password"更改為type="text" ,並在刪除占位符后將其更改回。

您也可以使用一長串的polyfill (部分:Web表單:輸入占位符)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM