首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用HTML和jQuery的水印文本框

使用HTML和jQuery的水印文本框
EN

Stack Overflow用户
提问于 2013-02-27 15:42:35
回答 1查看 2.8K关注 0票数 0

我想做一个水印文本框使用超文本标记语言和jQuery,但问题是,当我转到另一个页面,并再次回到水印文本框页面,水印文本显示为普通文本。

下面是我的jQuery代码:

代码语言:javascript
运行
复制
    <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代码:

代码语言:javascript
运行
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2013-02-27 15:52:58

尝试更改:

代码语言:javascript
运行
复制
if ($this.val() === '') {

代码语言:javascript
运行
复制
var txtVal = $.trim( $this.val() );
if ( txtVal === '') {

添加颜色,如

代码语言:javascript
运行
复制
...
$this.css({ 'opacity': '0.65' });
$this.css({ 'color': '#a8a8a8' });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15106621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档