placeholder jquery插件

jQuery Placeholder 1.1.9

Copyright (c) 2010 Michael J. Ryan (

Dual licensed under the MIT and GPL licenses:


Sets up a watermark for inputted fields... this will create a LABEL.watermark 
tag immediately following the input tag, the positioning will be set absolute, 
and it will be positioned to match the input tag.

To activate:


NOTE, when changing a value via script:

	$('#input_id').val('new value').change(); //force change event, so placeholder sets properly

To style the tags as appropriate (you'll want to make sure the font matches):

	label.placeholder {
		cursor: text;				<--- display a cursor to match the text input

		padding: 4px 4px 4px 4px;   <--- this should match the border+padding 
											for the input field(s)
		color: #999999;				<--- this will display as faded

You'll also want to have the color set for browsers with native support
	input:placeholder, textarea:placeholder {
		color: #999999;
	input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {
		color: #999999;


Thanks to...

	This works similar to the overlabel, but creates the actual label tag
	based on the placeholder attribute on the input tag, instead of 
	relying on the markup to provide it.


	var boundEvents = false;
	var default_options = {
		labelClass: 'placeholder'

	//check for native support for placeholder attribute, if so stub methods and return
	var input = document.createElement("input");
	if ('placeholder' in input) {
		$.fn.placeholder = $.fn.unplaceholder = function(){}; //empty function
		delete input; //cleanup IE memory
	delete input;

	//bind to resize to fix placeholders when the page resizes (fields are hidden/displayed, which can change positioning).

	$.fn.placeholder = function(options) {

		var opts = $.extend(default_options, options)

			var rnd=Math.random().toString(32).replace(/./,'')
				,label=$('<label style="position:absolute;display:none;top:0;left:0;"></label>');

			if (!input.attr('placeholder') || === ph) return; //already watermarked

			//make sure the input tag has an ID assigned, if not, assign one.
			if (!input.attr('id')) input.attr('id', 'input_' + rnd);

			label	.attr('id',input.attr('id') + "_placeholder")
					.data(ph, '#' + input.attr('id'))	//reference to the input tag
					.addClass(opts.labelClass + '-for-' + this.tagName.toLowerCase()) //ex: watermark-for-textarea

				.data(phl, '#' + label.attr('id'))	//set a reference to the label
				.data(ph,ph)		//set that the field is watermarked
				.addClass(ph)		//add the watermark class
				.after(label)		//add the label field to the page

			//setup overlay;;

	$.fn.unplaceholder = function(){
			var	input=$(this),

			if ( !== ph) return;


	function bindEvents() {
		if (boundEvents) return;

		//prepare live bindings if not already done.
		$("form").live('reset', function(){
			$(this).find('.' + ph).each(itemBlur);
		$('.' + ph)
		$('.' + phl)
			.live('click', function() {  $($(this).data(ph)).focus(); })
			.live('mouseup', function() {  $($(this).data(ph)).focus(); });
		bound = true;

		boundEvents = true;

	function itemChange() {
		var input = $(this);
		if (!!input.val()) {
		if ('FOCUSED') != 1) {

	function itemFocus() {

	function itemBlur() {
		var that = this;

		//use timeout to let other validators/formatters directly bound to blur/focusout work
			var input = $(that);

			//if the item wasn't refocused, test the item
			if ('FOCUSED') != 1) {
		}, 200);

	function showPHL(input, forced) {
		var label = $(;

		//if not already shown, and needs to be, show it.
		if ((forced || label.css('display') == 'none') && !input.val())
				.css('top', input.position().top + 'px')
				.css('left', input.position().left + 'px')
				.css('display', 'block');

		//console.dir({ 'input': { 'id':input.attr('id'), 'pos': input.position() }});

	var cr;
	function checkResize() {
		if (cr) window.clearTimeout(cr);
		cr = window.setTimeout(checkResize2, 50);
	function checkResize2() {
		$('.' + ph).each(function(){
			var input = $(this);
			var focused = $(this).data(ph+'FOCUSED');
			if (!focused) showPHL(input, true);

$('#email, #pwd, #re-pwd, #checkcode').placeholder();
via code