Académique Documents
Professionnel Documents
Culture Documents
<script type="text/javascript"
src="jquery.js"></script>
$(“li:odd”).prepend(‘<span>Changed</span>’).css({background:“red”});
If ($(‘#total’).attr(‘checked’)) {
//Traitement si cochée
}
else {
//Traitement si non cochée
}
jQuery example d’utilisation 4
$(document).ready(function() {
$(‘#ok’).submit(function() {
if ($(‘#login’).val() ==‘’) {
alert (‘Entrer un login’);
return false;
}
})
});
jQuery example d’utilisation 5
Effacer le contenu d’un champs de texte lorsqu’il
a le focus
<input name=“nom” type=“text” id=“nom”
value=“Entrez votre nom”>
$(‘#nom’).focus(function() {
var field = $(this);
field.val(‘’);
});
jQuery example d’utilisation 6
Tester le clic sur n’importe quel bouton
radio :
$(‘:radio’).click(function() {
//do stuff
});
Donner le focus au premier élément d’un
formulaire:
$(‘nom’).focus;
jQuery example d’utilisation 7
$(“span.none”).click( <div>
function(){
<span class=“all”>Select All</span>
$(this).siblings(“:checkbox”).removeAttr(“checked”);
} <span class=“none”>Select None</span>
);
<input name=“chk1” type=“checkbox”/>
<input name=“chk2” type=“checkbox”/>
$(“span.all”).click(
function(){ <input name=“chk3” type=“checkbox”/>
$(this).siblings(“:checkbox”).attr(“checked”,“checked”);
</div>
}
); <div>
<span class=“all”>Select All</span>
<span class=“none”>Select None</span>
or <input name=“chk4” type=“checkbox”/>
<input name=“chk5” type=“checkbox”/>
$(“span”).click(
<input name=“chk6” type=“checkbox”/>
function(){
if($(this).text()==“Select All”)) </div>
$(this).siblings(“:checkbox”).attr(“checked”,“checked”);
else if ($(this).attr(“class”)==“none”)
$(this).siblings(“:checkbox”).removeAttr(“checked”);
}
);
Conclusion
• jQuery est un framework complet et facile à utiliser
• Bibliothèque légère à charger
• Simplifier et unifie la syntaxe d’accès au DOM
• Permet de faire des requètes AJAX simplement
• UI et nombreux plugins complémentaires
20