jQuery 占位符插件 - 我做错了什么?

发布于 2024-12-27 15:59:32 字数 920 浏览 1 评论 0原文

我有很多这样的占位符:

<input placeholder="Placeholder">

但是,在 IE 中这会失败,我只有空白框。我在此处找到了一个假定的修复程序。

我完全按照指示将其包含在内:

<script>
$("'[placeholder]'").focus(function() {
var input = $(this);
if (input.val() == input.attr("'placeholder'")) {
input.val("''");
input.removeClass("'placeholder'");
}
}).blur(function() {
var input = $(this);
if (input.val() == "''" || input.val() == input.attr("'placeholder'")) {
input.addClass("'placeholder'");
input.val(input.attr("'placeholder'"));
}
}).blur();

$("'[placeholder]'").parents("'form'").submit(function() {
$(this).find("'[placeholder]'").each(function() {
var input = $(this);
if (input.val() == input.attr("'placeholder'")) {
  input.val("''");
}
})
});
</script>

它仍然无法生成占位符。我做错了什么?

I have a lot of placeholders like so:

<input placeholder="Placeholder">

However, in IE this fails and I just have blank boxes. I found a supposed fix here.

I include it exactly as indicated:

<script>
$("'[placeholder]'").focus(function() {
var input = $(this);
if (input.val() == input.attr("'placeholder'")) {
input.val("''");
input.removeClass("'placeholder'");
}
}).blur(function() {
var input = $(this);
if (input.val() == "''" || input.val() == input.attr("'placeholder'")) {
input.addClass("'placeholder'");
input.val(input.attr("'placeholder'"));
}
}).blur();

$("'[placeholder]'").parents("'form'").submit(function() {
$(this).find("'[placeholder]'").each(function() {
var input = $(this);
if (input.val() == input.attr("'placeholder'")) {
  input.val("''");
}
})
});
</script>

It still fails to produce placeholders. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光倒影 2025-01-03 15:59:32

这是我在项目中使用的内容(缩小后仅 753 字节):

(function($)
{
    // check for native placeholder support. Borrowed from Modernizr.
    var placeholderSupport = !!( 'placeholder' in document.createElement('input') );

    $.fn.placeholder = function(text)
    {
        // if placeholder is supported natively, exit
        if (placeholderSupport) return this;

        // else:
        return this.each(function()
        {
            var $this = $(this);

            text = text || $this.attr('placeholder');

            // if it's not a input element, exit
            if ( this.tagName !== "INPUT" )
            {
                $.error('jquery.placeholder only works on "input" elements. Does not support "' + this.tagName.toLowerCase() + '" elements.');
                return;
            }

            // If placeholder has already been applied, exit
            if ( $this.data('jquery-placeholder')  ) return;

            // If not, let's mark it now as having placeholder applied
            $this.data('jquery-placeholder', true);

            // if its value is empty, let's add the placeholder text
            if ( $this.val() === '' )
            {
                $this.val( text ).addClass('placeholder');
            }

            // Now, let's setup the focus & blur events, to add and remove the text & the class, respectively.
            $this.focus(function()
            {
                var $this = $(this);
                if ( $this.val() === text && $this.hasClass('placeholder') )
                {
                    $this.val('').removeClass('placeholder');
                }
            })
            .blur(function()
            {
                var $this = $(this);
                if ( $this.val() === '')
                {
                    $this.val( text ).addClass('placeholder');
                }
            });

        });
    };

    // Now, before we leave, let's just make sure that these placeholder values never get submitted
    placeholderSupport || $(document).delegate('form', 'submit', function()
    {
        $(this)
            .find('.placeholder')
            .val('').removeClass('placeholder');
    });

})(jQuery);

这是缩小版本:

(function(b){var d=!!("placeholder"in document.createElement("input"));b.fn.placeholder=function(c){return d?this:this.each(function(){var a=b(this);c=c||a.attr("placeholder");"INPUT"!==this.tagName?b.error('jquery.placeholder only works on "input" elements. Does not support "'+this.tagName.toLowerCase()+'" elements.'):a.data("jquery-placeholder")||(a.data("jquery-placeholder",!0),""===a.val()&&a.val(c).addClass("placeholder"),a.focus(function(){var a=b(this);a.val()===c&&a.hasClass("placeholder")&&a.val("").removeClass("placeholder")}).blur(function(){var a=b(this);""===a.val()&&a.val(c).addClass("placeholder")}))})};d||b(document).delegate("form","submit",function(){b(this).find(".placeholder").val("").removeClass("placeholder")})})(jQuery);

然后,要使用它,只需在准备好的文档上运行它:

$('input[placeholder]').placeholder();

这是小提琴:http://jsfiddle.net/vNkPD

Here's what I use in my projects (Just 753 bytes minified):

(function($)
{
    // check for native placeholder support. Borrowed from Modernizr.
    var placeholderSupport = !!( 'placeholder' in document.createElement('input') );

    $.fn.placeholder = function(text)
    {
        // if placeholder is supported natively, exit
        if (placeholderSupport) return this;

        // else:
        return this.each(function()
        {
            var $this = $(this);

            text = text || $this.attr('placeholder');

            // if it's not a input element, exit
            if ( this.tagName !== "INPUT" )
            {
                $.error('jquery.placeholder only works on "input" elements. Does not support "' + this.tagName.toLowerCase() + '" elements.');
                return;
            }

            // If placeholder has already been applied, exit
            if ( $this.data('jquery-placeholder')  ) return;

            // If not, let's mark it now as having placeholder applied
            $this.data('jquery-placeholder', true);

            // if its value is empty, let's add the placeholder text
            if ( $this.val() === '' )
            {
                $this.val( text ).addClass('placeholder');
            }

            // Now, let's setup the focus & blur events, to add and remove the text & the class, respectively.
            $this.focus(function()
            {
                var $this = $(this);
                if ( $this.val() === text && $this.hasClass('placeholder') )
                {
                    $this.val('').removeClass('placeholder');
                }
            })
            .blur(function()
            {
                var $this = $(this);
                if ( $this.val() === '')
                {
                    $this.val( text ).addClass('placeholder');
                }
            });

        });
    };

    // Now, before we leave, let's just make sure that these placeholder values never get submitted
    placeholderSupport || $(document).delegate('form', 'submit', function()
    {
        $(this)
            .find('.placeholder')
            .val('').removeClass('placeholder');
    });

})(jQuery);

Here is the minified version:

(function(b){var d=!!("placeholder"in document.createElement("input"));b.fn.placeholder=function(c){return d?this:this.each(function(){var a=b(this);c=c||a.attr("placeholder");"INPUT"!==this.tagName?b.error('jquery.placeholder only works on "input" elements. Does not support "'+this.tagName.toLowerCase()+'" elements.'):a.data("jquery-placeholder")||(a.data("jquery-placeholder",!0),""===a.val()&&a.val(c).addClass("placeholder"),a.focus(function(){var a=b(this);a.val()===c&&a.hasClass("placeholder")&&a.val("").removeClass("placeholder")}).blur(function(){var a=b(this);""===a.val()&&a.val(c).addClass("placeholder")}))})};d||b(document).delegate("form","submit",function(){b(this).find(".placeholder").val("").removeClass("placeholder")})})(jQuery);

Then, to use it, simply run this on document ready:

$('input[placeholder]').placeholder();

Here's the fiddle: http://jsfiddle.net/vNkPD

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文