【问题标题】:Placeholders in IEIE 中的占位符
【发布时间】:2012-02-20 08:08:02
【问题描述】:

我正在使用这个脚本:http://www.morethannothing.co.uk/wp-content/uploads/2010/01/placeholder.js 将一些占位符放入 IE。适用于文本和密码的输入类型。

但似乎没有为文本区域加载。有谁知道我可以添加一行代码,或者执行一些 jQuery 或 JavaScript 来加载它。

提前致谢。

【问题讨论】:

  • jQuery 占位符插件是一毛钱,你可能想直接切换到支持textareas 的。这些都可以:place5(我的),jquery-placeholderjquery-placeholder (2)
  • 同意。占位符插件是一个很好的发现并且非常有用。

标签: javascript jquery internet-explorer placeholder


【解决方案1】:

$(':text[placeholder],:password[placeholder],textarea[placeholder]')代替$(':text[placeholder],:password[placeholder]')

【讨论】:

  • 你永远不应该使用:text:password 或任何其他 jQuery 组成的选择器(如果你能提供帮助的话),因为不要使用浏览器的 fast,原生选择器引擎,Sizzle 必须通过迭代(在本例中)DOM 中的所有元素来完成所有工作。
【解决方案2】:

对于所有输入和文本区域: $('input[placeholder],textarea[placeholder]')

【讨论】:

    【解决方案3】:
    <script type="text/javascript"> 
        if(navigator.userAgent.indexOf("MSIE") > 0 )
        {
            $(function()
            {
                var input = document.createElement("input");
                if(('placeholder' in input) == false)
                {
                    $('[placeholder]').focus(function()
                    {
                        var i = $(this);
                        if(i.val() == i.attr('placeholder'))
                        {
                            i.val('').removeClass('placeholder');
                            if(i.hasClass('password'))
                            {
                                i.removeClass('password'); this.type='password';
                            }
                        }
                    }).blur(function()
                    {
                        var i = $(this);
                        if(i.val() == '' || i.val() == i.attr('placeholder'))
                        {
                            if(this.type=='password')
                            {
                                i.addClass('password'); this.type='text';
                            }
                            i.addClass('placeholder').val(i.attr('placeholder'));
                        }
                    }).blur().parents('form').submit(function()
                    {
                        $(this).find('[placeholder]').each(function()
                        {
                            var i = $(this);
                            if(i.val() == i.attr('placeholder')) i.val('');
                        });
                    });
                }
            });
        }
    
    
        </script>
    

    【讨论】:

      猜你喜欢
      • 2014-07-13
      • 2019-04-09
      • 1970-01-01
      • 2013-09-28
      • 2016-03-19
      • 2013-09-01
      • 2014-02-03
      • 2016-01-07
      • 1970-01-01
      相关资源
      最近更新 更多