码迷,mamicode.com
首页 > Web开发 > 详细

jquery给input赋初始值,聚焦时清空

时间:2014-09-30 11:02:32      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   使用   ar   java   for   

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="js/jquery-1.8.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        
        $(function () {
            //取出有clear类的input域
            //(注: "clear once" 是两个class clear 和 once)
            $(#testform input.clear).each(function () {
                //使用data方法存储数据
                $(this).data("txt", $.trim($(this).val()));
            }).focus(function () {
                // 获得焦点时判断域内的值是否和默认值相同,如果相同则清空
                if ($.trim($(this).val()) === $(this).data("txt")) {
                    $(this).val("");
                }
            }).blur(function () {
                // 为有class clear的域添加blur时间来恢复默认值
                // 但如果class是once则忽略
                if ($.trim($(this).val()) === "" && !$(this).hasClass("once")) {
                    //Restore saved data
                    $(this).val($(this).data("txt"));
                }
            });
        });
    </script>
</head>
<body>
    <form id="testform">
        <input type="text" class="clear" value="Always cleared" />
        <input type="text" class="clear once" value="Cleared only once" />
        <input type="text" value="Normal text" />
    </form>
</body>
</html>

 

jquery给input赋初始值,聚焦时清空

标签:style   blog   http   color   io   使用   ar   java   for   

原文地址:http://www.cnblogs.com/xyangs/p/4001292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!