我自己做了一个,所以它完全符合我的需求:http: //jsfiddle.net/42t6R/2/。
这很简单,但效果很好。
编辑:错误较少的新版本,为什么不将其作为插件提交:)
http://plugins.jquery.com/project/fadehints
http://jsfiddle.net/9rgHg/2/
(function( $, undefined ) {
$.fn.fadehints = function( data, speed ) {
var i = 0;
var $this = $( this );
var offset = $this.offset();
var $input = $("<input>").css( "position", "absolute" )
.css( "left", offset.left )
.css( "top", offset.top )
.css( "background-color", "transparent" )
.css( "color", "gray" )
.css( "border", 0 )
.css( "padding", 2 )
.css( "font-size", $this.css( "font-size" ) )
.css( "font-family", $this.css( "font-family" ) );
var $parent = $this.parent();
var $div = $( "<div>" ).append( $this.detach(), $input );
var change = function() {
if( i >= data.length ) {
i = 0;
}
$input.hide().val( data[i] ).fadeIn( 1000 );
i++;
};
$this.bind( "focus keydown", function(e) {
if( !( e.bubbles == null ) ) { // Only clear if event was triggered by user
window.clearInterval( interval );
$input.hide();
}
} );
$input.bind( "click focus", function() {
window.clearInterval( interval );
$this.focus(); // $this === the real textbox
$( this ).hide(); // $(this) === the overlap textbox
} );
$this.click( function() {
$input.hide();
window.clearInterval( interval );
} );
$this.blur( function() {
window.clearInterval( interval );
if( $this.val() === "" && $this[0] !== document.activeElement ) {
if( !$input.is(":visible")) {
change();
}
interval = window.setInterval( change, speed );
}
} );
$parent.append( $div );
change(true);
var interval = window.setInterval( change, speed );
return $this;
};
})(jQuery);
$(function() {
$('#tb').fadehints([
"test1", "test2"
]);
});