<!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>
data存储数据DEMO 获得光标文本消失
最新推荐文章于 2025-08-24 15:22:07 发布