data存储数据DEMO 获得光标文本消失

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Expires" content="Fri, Jan 01 1900 00:00:00 GMT">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Lang" content="en">
<meta name="author" content="">
<meta http-equiv="Reply-to" content="@.com">
<meta name="generator" content="PhpED 5.6">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="creation-date" content="11/11/2008">
<meta name="revisit-after" content="15 days">
<title>Untitled</title>

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

<form id="testform">
    <input class="clear" type="text" value="Always cleared" />
    <input class="clear once" type="text" value="Cleared only once" />
    <input type="text" value="Normal text" />
</form>

<script type="text/javascript">
jQuery.fn.log = function (msg) {
console.log("%s: %o", msg, this);
return this;
};
  
$(function() {
    //Go through every input field with clear class using each function
    //(NB! "clear once" is two classes clear and once)
    $('#testform input.clear').each(function(){
        //use the data function to save the data
        $(this).data( "txt", $.trim($(this).val()) );
    }).focus(function(){
        // On focus test for default saved value and if not the same clear it
        if ( $.trim($(this).val()) === $(this).data("txt") ) {
            $(this).val("");
        }
    }).blur(function(){
        // Use blur event to reset default value in field that have class clear
        // but ignore if class once is present
        if ( $.trim($(this).val()) === "" && !$(this).hasClass("once") ) {
            //Restore saved data
            $(this).val( $(this).data("txt") );
        }
    });
});  



var input_text = $('#shopping_cart_items input.text');
input_text
    .css('border', '3px dashed yellow')
    .css('background-color', 'red')
    .val("text updated");  
</script>

</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值