Utilisez l'espace réservé.
<input type="text" id="search" placeholder="Search..." />
Support de navigateurChrome
Firefox 3.7 +
Internet Explorer 9 +
Avant Browser(Not sure)
Opéra (quelque part autour de 11 v)
Safari 4 +
Évidemment pas tout le monde s'exécute un navigateur HTML5, pourtant, donc allaient avoir besoin de ce faux pour les navigateurs autres, inférieures.
Ajouter à la tête de chaque page, ou de l'ajouter à un script distinct et l'appeler sur chaque page :
<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>
Ce que le code ci-dessus, est vérifiez si le navigateur est compatible avec l'espace réservé. Si elle n'est pas le cas, il imite l'effet à l'aide de CSS. La valeur de la couleur à l'intérieur de la .hasplaceholder est la couleur du texte truqué espace réservé à l'intérieur de votre zone de texte. #777 est la valeur d'espace réservé par défaut.
Maintenant, chaque navigateur unique qui prend en charge les CSS peut utiliser cette fonctionnalité.Un exemple complet :
<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>
Exemple :
lienEspère que cette aide, jouir.
Every job is a self-portrait of the person who did it: Autograph your work with excellence.