javascript - placeholder-not-working-for-internet-explorer 如果输入类型是密码,仍然显示 *********

标签 javascript

我在Placeholder not working for Internet Explorer得到了1个答案

我正在使用这段代码,

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 属性。此代码(来 self 的 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 中采用的路线),否则我担心你将不得不忍受它。

关于javascript - placeholder-not-working-for-internet-explorer 如果输入类型是密码,仍然显示 *********,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12741700/

相关文章:

javascript - 当滚动点超过 div 顶部时功能不启动

javascript - 在页面加载时获取 safe-area-inset-bottom

javascript - Firefox 和 IE 对待图像对象的方式与 Webkit 有何不同?

javascript - 尚未为上下文 : _. 加载 TypeScript 导出和导入模块名称使用 require([])

javascript - 向jsPDF添加内容时获取当前x,y位置

javascript - 用JS解析JSON嵌套数组

javascript - 使用javascript拆分文本

javascript - 如何从 Rest API 获取 JSON 数据

javascript - Chrome 和 Firefox 中的 SVG 文本对齐不一致

javascript - 如果对象数组中的对象设置为未定义,则返回 true