Posté par Cedric, le 15/12/2008 - Technologie
Si vous utilisez jQuery, vous pratiquez certainement la manipulation du DOM. Par exemple, lorsque vous créez des objets à la volée, le DOM se trouve modifié. Malheureusement, jQuery n'intègre pas automatiquement les éléments que vous rajoutez à sa surveillance (listeners).
Travaillons sur un bout de code basique:
<input type="button" id="add" value="add another radio button" />
<input type="radio" name="cool" value="cedric" />
<script language="JavaScript">
$(document).ready(function(){
$(":radio").click(function(){
alert($(this).val());
})
$("#add").click(function(e){
e.preventDefault();
var $v = Math.random(1, 100);
$("#container").append(' value:'+$v+'
');
})
})
</script>
Dans cet exemple, le click sur les éléments que vous ajoutez au fur et à mesure ne déclenche aucune action nativement (à l'inverse de l'élément créé à la base, avec "cedric" comme valeur). C'est un gros problème quand on souhaite ajouter du dynamisme à ses documents tout en bénéficiant des écouteurs globaux.
$(":radio").click(function(){
alert($(this).val());
});
Après:
$(":radio").livequery('click', function(){
alert($(this).val());
});
Et vous pouvez ajouter autant d'éléments identiques au DOM, ils répondront tous de la même manière lorsque vous déclenchez un événement dessus.
tags: jquery
Commentaires
Posted by lacewigs on 12/06/2010
Posted by dahai on 20/06/2010
Posted by xiaoxiao on 20/06/2010
Posted by prada handbag on 07/07/2010
Posted by lacewigs on 07/07/2010
Posted by mbt on 08/07/2010
Posted by rayallen on 13/07/2010
Posted by nike shoes on sale on 21/07/2010
But the chi hair straightener is that with proper styling,short chi hair dryer can look fabulous! Short cheap hair straighteners is easy to maintain and is healthier pink hair straighteners.There are a variety of short layered cheap straighteners with different styling best hair straightener that can make women look younger.The most popular short layered pink mk4 is the short layered bob.There are many ghd mk4 to it and using colors and highlights can accentuate the mk4 straighteners.
Posted by GHD straighteners on 22/07/2010
Posted by NFL jerseys on 22/07/2010
They are designed to give your MBT Chapa the motion of walking on MBT Changa,a movement that requires the MBT Tataga of almost every muscle from your MBT Fora to your neck,forcing you to continually readjust your MBT Fanaka to gain its center of MBT Kaya.Take a look at people who walk at least 30 MBT Tunisha a day. Those who walk on the MBT Tembea become toned fasted than those who walk on flat,solid MBT VOI.
Posted by MBT shoes on 22/07/2010
Posted by UGG boots on 22/07/2010
Posted by cosplay on 30/07/2010
Posted by essay service on 11/03/2010