Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A jQuery plugin that enables HTML5 placeholder behavior for browsers that aren’t trying hard enough yet

License

NotificationsYou must be signed in to change notification settings

mathiasbynens/jquery-placeholder

Repository files navigation

Demo & Examples

https://mathiasbynens.github.io/jquery-placeholder/

Example Usage

HTML

<inputtype="text"name="name"placeholder="e.g. John Doe"><inputtype="email"name="email"placeholder="e.g. address@example.ext"><inputtype="url"name="url"placeholder="e.g. https://mathiasbynens.be/"><inputtype="tel"name="tel"placeholder="e.g. +32 472 77 69 88"><inputtype="password"name="password"placeholder="e.g. h4x0rpr00fz"><inputtype="search"name="search"placeholder="Search this site…"><textareaname="message"placeholder="Your message goes here"></textarea>

jQuery

Use the plugin as follows:

$('input, textarea').placeholder();

By default,.placeholder css class will be added. You can override default by passing thecustomClass option:

$('input, textarea').placeholder({customClass:'my-placeholder'});

You’ll still be able to usejQuery.val() to get and set the input values. If the element is currently showing a placeholder,.val() will return an empty string instead of the placeholder text, just like it does in browsers with a native@placeholder implementation. Calling.val('') to set an element’s value to the empty string will result in the placeholder text (re)appearing.

CSS

The plugin automatically addsclass="placeholder" to the elements who are currently showing their placeholder text. You can use this to style placeholder text differently:

input,textarea {color:#000; }.placeholder {color:#aaa; }

I’d suggest sticking to the#aaa color for placeholder text, as it’s the default in most browsers that support@placeholder. If you really want to, though, you canstyle the placeholder text in some of the browsers that natively support it.

Installation

You can install jquery-placeholder by usingBower.

bower install jquery-placeholder

Or you can install it throughnpm:

npm install --save jquery-placeholder

Contributors should install the »dev dependencies« after forking and cloning vianpm.

npm install

Notes

  • Requires jQuery 1.6+. For an older version of this plugin that works under jQuery 1.4.2+, seev1.8.7.

  • Works in all A-grade browsers, including IE6.

  • Automatically checks if the browser natively supports the HTML5placeholder attribute forinput andtextarea elements. If this is the case, the plugin won’t do anything. If@placeholder is only supported forinput elements, the plugin will leave those alone and apply totextareas exclusively. (This is the case for Safari 4, Opera 11.00, and possibly other browsers.)

  • Caches the results of its two feature tests injQuery.fn.placeholder.input andjQuery.fn.placeholder.textarea. For example, if@placeholder is natively supported forinput elements,jQuery.fn.placeholder.input will betrue. After loading the plugin, you can re-use these properties in your own code.

  • Using<input type="reset" /> will break the plugin functionality

  • Makes sure it never causes duplicate IDs in your DOM, even in browsers that need an extrainput element to fake@placeholder for password inputs. This means you can safely do stuff like:

    <labelfor="bar">Example label</label><inputtype="password"placeholder="foo"id="bar">

    And the<label> will always point to the<input> element you’d expect. Also, all CSS styles based on the ID will just work™.

License

This plugin is available underthe MIT license.

Thanks to…

Mathias

About

A jQuery plugin that enables HTML5 placeholder behavior for browsers that aren’t trying hard enough yet

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp