【问题标题】:How to fix placeholder for Internet Explorer?如何修复 Internet Explorer 的占位符?
【发布时间】:2013-07-20 13:37:40
【问题描述】:

我想为文本和密码输入字段设置占位符。除了 Internet Explorer,我可以在任何东西上做到这一点。如何才能做到这一点?也许jQuery?特别是对于密码,我希望占位符说“密码”。一旦点击输入,其中的文本将显示为密码文本。谢谢! :)

【问题讨论】:

标签: jquery html passwords placeholder


【解决方案1】:

IE9+ 应该支持占位符,但我发现事实并非如此。有几种方法可以“假装”有占位符。我发现在 IE 上具有占位符外观/感觉的最佳方法是创建一个 jquery 函数,该函数在输入框后面添加一个 div(使输入框透明)以及您要显示的文本。然后在输入或数据时显示:无 div 或不透明输入框。

其他人将文本放在输入字段中,但是验证和密码字段可能会很麻烦。

类似:

// Check browser type so we don't mess with other browsers that do this right
if (BrowserDetect.browser === 'Explorer') {
    // Find all placeholders
    $('[placeholder]').each(function(_index, _this) {
        var $this = $(this);
        var id = this.id;
        var thisValue = $this.val();
        var value = $this.attr('placeholder');
        var $element = $('#' + id + '_ph');
        if ( $element.length === 0 ) {
            // Add placeholder if no input you want to add it even if value in case they remove value
            $this.attr('placeholder', '');
            // Add the div (make sure you style ie_placeholder_fix
            // class with correct positioning etc)
            $('<div id="' + id + '_ph" class="ie_placeholder_fix">' + value + '</div>').insertAfter($this);
        }
        //Maybe you want to hide if it has a value?
        if ( thisValue ) { $element.hide(); } else { $element.show(); }
        $this.blur(checkForInput);
    });
}

您只需要检查它们是否有值,如果预先填充了值(即可编辑的表单)是否隐藏 div

【讨论】:

    猜你喜欢
    • 2013-11-12
    • 2011-07-28
    • 2012-01-27
    • 2014-11-24
    • 2017-07-23
    • 1970-01-01
    • 2019-11-11
    • 2012-09-08
    相关资源
    最近更新 更多