Utilizar el marcador de posición.
<input type="text" id="search" placeholder="Search..." />
Soporte de navegadorChrome
Firefox 3.7 +
Internet Explorer 9 +
Avant Browser(Not sure)
Opera (en algún lugar alrededor de 11 v)
Safari 4 +
Obviamente no todo el mundo está ejecutando un explorador HTML5, todavía, por lo que se va a necesitar falsificar para los exploradores de otros, inferiores.
Añadir esto a la cabeza de cada página, o agregar a una secuencia de comandos separado y llamar en cada página:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
// This adds 'placeholder' to the items listed in the jQuery .support object.
jQuery(function() {
jQuery.support.placeholder = false;
test = document.createElement('input');
if('placeholder' in test) jQuery.support.placeholder = true;
});
// This adds placeholder support to browsers that wouldn't otherwise support it.
$(function() {
if(!$.support.placeholder) {
var active = document.activeElement;
$(':text').focus(function () {
if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('hasPlaceholder');
}
}).blur(function () {
if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
}
});
$(':text').blur();
$(active).focus();
$('form:eq(0)').submit(function () {
$(':text.hasPlaceholder').val('');
});
}
});
</script>
<style>
.hasPlaceholder {
color: #777;
}
</style>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
- <script>
- // This adds 'placeholder' to the items listed in the jQuery .support object.
- jQuery(function() {
- jQuery.support.placeholder = false;
- test = document.createElement('input');
- if('placeholder' in test) jQuery.support.placeholder = true;
- });
- // This adds placeholder support to browsers that wouldn't otherwise support it.
- $(function() {
- if(!$.support.placeholder) {
- var active = document.activeElement;
- $(':text').focus(function () {
- if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
- $(this).val('').removeClass('hasPlaceholder');
- }
- }).blur(function () {
- if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
- $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
- }
- });
- $(':text').blur();
- $(active).focus();
- $('form:eq(0)').submit(function () {
- $(':text.hasPlaceholder').val('');
- });
- }
- });
- </script>
- <style>
- .hasPlaceholder {
- color: #777;
- }
- </style>
Lo que el código anterior hace, es comprobar si el navegador es compatible con el marcador de posición. Si no es así, imita el efecto mediante CSS. El valor de color dentro de .hasplaceholder es el color del texto de marcador de posición falsificado dentro de su cuadro de texto. #777 es el valor predeterminado de marcador de posición.
Ahora cada explorador que admite CSS puede utilizar esta función.Un ejemplo completo:
<html>
<head>
<title>Placeholder Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
// This adds 'placeholder' to the items listed in the jQuery .support object.
jQuery(function() {
jQuery.support.placeholder = false;
test = document.createElement('input');
if('placeholder' in test) jQuery.support.placeholder = true;
});
// This adds placeholder support to browsers that wouldn't otherwise support it.
$(function() {
if(!$.support.placeholder) {
var active = document.activeElement;
$(':text').focus(function () {
if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('hasPlaceholder');
}
}).blur(function () {
if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
}
});
$(':text').blur();
$(active).focus();
$('form:eq(0)').submit(function () {
$(':text.hasPlaceholder').val('');
});
}
});
</script>
<style>
.hasPlaceholder {
color: #777;
}
</style>
</head>
<body>
<input type="text" name="search" placeholder="Search here..." />
</body>
</html>
- <html>
- <head>
- <title>Placeholder Example</title>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
- <script>
- // This adds 'placeholder' to the items listed in the jQuery .support object.
- jQuery(function() {
- jQuery.support.placeholder = false;
- test = document.createElement('input');
- if('placeholder' in test) jQuery.support.placeholder = true;
- });
- // This adds placeholder support to browsers that wouldn't otherwise support it.
- $(function() {
- if(!$.support.placeholder) {
- var active = document.activeElement;
- $(':text').focus(function () {
- if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
- $(this).val('').removeClass('hasPlaceholder');
- }
- }).blur(function () {
- if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
- $(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
- }
- });
- $(':text').blur();
- $(active).focus();
- $('form:eq(0)').submit(function () {
- $(':text.hasPlaceholder').val('');
- });
- }
- });
- </script>
- <style>
- .hasPlaceholder {
- color: #777;
- }
- </style>
- </head>
- <body>
- <input type="text" name="search" placeholder="Search here..." />
- </body>
- </html>
Ejemplo:
LINKDe este modo, disfrutar de la esperanza.
Every job is a self-portrait of the person who did it: Autograph your work with excellence.