0

我想使用 HTML 和 jQuery 制作一个水印文本框,但问题是当我转到另一个页面并再次返回水印文本框页面时,水印文本以普通文本显示。

下面是我的 jQuery 代码:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
</script>
<script type="text/javascript">

    (function ($) {
        $.fn.extend({
            inputWatermark: function () {
                return this.each(function () {
                    // retrieve the value of the ‘placeholder’ attribute
                    var watermarkText = $(this).attr('placeholder');
                    var $this = $(this);
                    if ($this.val() === '') {
                        $this.val(watermarkText);
                        // give the watermark a translucent look
                        $this.css({ 'opacity': '0.65' });
                    }



                    $this.blur(function () {
                        if ($this.val() === '') {
                            // If the text is empty put the watermark
                            // back

                            $this.val(watermarkText);
                            // give the watermark a translucent look
                            $this.css({ 'opacity': '0.65' });
                        }
                    });



                    $this.focus(function () {
                        if ($this.val() === watermarkText) {
                            $this.val('');
                            $this.css({ 'opacity': '1.0' });
                        }
                    });
                });
            }
        });

    })(jQuery);
</script>

这是我的 HTML 代码:

<body>
<input id="input1" placeholder="Placeholder here..." />
<input id="input2" placeholder="2nd Placeholder" />
<a href="../../back.htm">google</a>
<script type="text/javascript">

    $(document).ready(function () {
        $(':input').inputWatermark();
    });

</script></body>
4

1 回答 1

1

尝试改变:

if ($this.val() === '') {

var txtVal = $.trim( $this.val() );
if ( txtVal === '') {

添加颜色,例如

...
$this.css({ 'opacity': '0.65' });
$this.css({ 'color': '#a8a8a8' });
于 2013-02-27T07:52:57.943 回答