Je souhaite que le champ de recherche de ma page Web affiche le mot "Rechercher" en italique gris. Lorsque la zone reçoit le focus, elle doit ressembler à une zone de texte vide. S'il contient déjà du texte, il devrait afficher le texte normalement (noir, non italique). Cela m'aidera à éviter l'encombrement en retirant l'étiquette.
BTW, c'est une recherche Ajax sur la page , donc elle n'a pas de bouton.
javascript
html
Michael L Perry
la source
la source
Réponses:
Une autre option, si vous êtes heureux de n'avoir cette fonctionnalité que pour les navigateurs plus récents, est d'utiliser la prise en charge offerte par l' attribut d' espace réservé de HTML 5 :
En l'absence de styles, dans Chrome, cela ressemble à:
Vous pouvez essayer des démos ici et dans le style d'espace réservé HTML5 avec CSS .
Assurez-vous de vérifier la compatibilité du navigateur de cette fonctionnalité . Le support dans Firefox a été ajouté dans 3.7. Chrome va bien. Internet Explorer n'a ajouté la prise en charge qu'en 10. Si vous ciblez un navigateur qui ne prend pas en charge les espaces réservés d'entrée, vous pouvez utiliser un plug-in jQuery appelé jQuery HTML5 Placeholder , puis ajoutez simplement le code JavaScript suivant pour l'activer.
la source
C'est ce qu'on appelle un filigrane de zone de texte, et cela se fait via JavaScript.
ou si vous utilisez jQuery, une bien meilleure approche:
la source
Vous pouvez définir l' espace réservé à l'aide de l'
placeholder
attribut en HTML ( prise en charge du navigateur ). Lefont-style
etcolor
peut être modifié avec CSS (bien que la prise en charge du navigateur soit limitée).la source
You can add and remove a special CSS class and modify the input value
onfocus
/onblur
with JavaScript:Then specify a hint class with the styling you want in your CSS for example:
la source
The best way is to wire up your JavaScript events using some kind of JavaScript library like jQuery or YUI and put your code in an external .js-file.
But if you want a quick-and-dirty solution this is your inline HTML-solution:
Updated: Added the requested coloring-stuff.
la source
I posted a solution for this on my website some time ago. To use it, import a single
.js
file:Then annotate whatever inputs you want to have hints with the CSS class
hintTextbox
:More information and example are available here.
la source
Voici un exemple fonctionnel avec le cache de la bibliothèque Google Ajax et un peu de magie jQuery.
Ce serait le CSS:
Ce serait le code JavaScript:
Et ce serait le HTML:
J'espère que c'est suffisant pour vous intéresser à la fois aux GAJAXLibs et à jQuery.
la source
Now it become very easy. In html we can give the placeholder attribute for input elements.
e.g.
check for more details :http://www.w3schools.com/tags/att_input_placeholder.asp
la source
For jQuery users: naspinski's jQuery link seems broken, but try this one: http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
You get a free jQuery plugin tutorial as a bonus. :)
la source
I found the jQuery plugin jQuery Watermark to be better than the one listed in the top answer. Why better? Because it supports password input fields. Also, setting the color of the watermark (or other attributes) is as easy as creating a
.watermark
reference in your CSS file.la source
This is called "watermark".
I found the jQuery plugin jQuery watermark which, unlike the first answer, does not require extra setup (the original answer also needs a special call to before the form is submitted).
la source
Use jQuery Form Notifier - it is one of the most popular jQuery plugins and doesn't suffer from the bugs some of the other jQuery suggestions here do (for example, you can freely style the watermark, without worrying if it will get saved to the database).
jQuery Watermark uses a single CSS style directly on the form elements (I noticed that CSS font-size properties applied to the watermark also affected the text boxes -- not what I wanted). The plus with jQuery Watermark is you can drag-drop text into fields (jQuery Form Notifier doesn't allow this).
Another one suggested by some others (the one at digitalbrush.com), will accidentally submit the watermark value to your form, so I strongly recommend against it.
la source
Use a background image to render the text:
Then all you have to do is detect
value == 0
and apply the right class:And the jQuery JavaScript code looks like this:
la source
You could easily have a box read "Search" then when the focus is changed to it have the text be removed. Something like this:
<input onfocus="this.value=''" type="text" value="Search" />
Of course if you do that the user's own text will disappear when they click. So you probably want to use something more robust:
la source
When the page first loads, have Search appear in the text box, colored gray if you want it to be.
When the input box receives focus, select all of the text in the search box so that the user can just start typing, which will delete the selected text in the process. This will also work nicely if the user wants to use the search box a second time since they won't have to manually highlight the previous text to delete it.
la source
I like the solution of "Knowledge Chikuse" - simple and clear. Only need to add a call to blur when the page load is ready which will set the initial state:
la source
You want to assign something like this to onfocus:
and this to onblur:
(You can use something a bit cleverer, like a framework function, to do the classname switching if you want.)
With some CSS like this:
la source
I'm using a simple, one line javascript solution which works great. Here is an example both for textbox and for textarea:
only "downside" is to validate at $_POST or in Javascript validation before doing anything with the value of the field. Meaning, checking that the field's value isn't "Text".
la source
la source
Simple Html 'required' tag is useful.
It specifies that an input field must be filled out before submitting the form or press the button submit. Here is example
la source
Utilisateur AJAXToolkit de http://asp.net
la source