用户在输入字段中单击并且值消失 [英] User clicks in an input field and the value disappear

查看:95
本文介绍了用户在输入字段中单击并且值消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索一个脚本以消除输入字段中的值。
用户点击进入,该值消失,如果用户没有在输入字段中写入内容,则应该再次显示文本。

I search a script to disappear the value in an input field. A user click into and the value disappear and if the user doesn't write something into the input field it should be appear the text again.

我试试它使用jQuery和focusin()focusout(),但是然后我改变了所有输入字段的值。

I try it with jQuery and focusin() focusout() but then I change the value from all input field.

推荐答案

为此或者您可以使用值作为占位符。请检查出来

You can use placeholder for this or else you can use value as placeholder. Just check it out

占位符价值

jQuery(document).ready(function(){
    jQuery("input[type='text']").each(function(){
        var x = jQuery(this).attr("value");
        jQuery(this).focus(function(){
            if($(this).val()==x)
            {
                $(this).val('');
            }       
        });
        jQuery(this).blur(function(){
            if($(this).val()=="")
            {
                $(this).val(x);
            }
        });
    });
});

使用占位符

<input type="text" placeholder="test">

这篇关于用户在输入字段中单击并且值消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆