<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JQuery, PHP, html, design... // arnaud-k : un blog de geek &#187; astuces</title>
	<atom:link href="http://blog.arnaud-k.fr/tag/astuces/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.arnaud-k.fr</link>
	<description>Le blog JQuery, Html, Web Design</description>
	<lastBuildDate>Thu, 21 Apr 2011 08:10:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>5 conseils pour développer avec JQuery</title>
		<link>http://blog.arnaud-k.fr/2009/10/26/5-conseils-pour-developper-avec-jquery/</link>
		<comments>http://blog.arnaud-k.fr/2009/10/26/5-conseils-pour-developper-avec-jquery/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 07:00:26 +0000</pubDate>
		<dc:creator>arnaud</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[astuces]]></category>
		<category><![CDATA[exemple de JQuery]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://blog.arnaud-k.fr/?p=1492</guid>
		<description><![CDATA[Pour prolonger la série des tutorials et conseils sur JQuery, je propose un article pour livrer quelques astuces que j&#8217;ai découvert par moi-même ou glanées sur le web dans le but de mieux coder et d&#8217;optimiser le code JQuery. En&#160;[...]</p> <p class="read-more"><a class="gray normal" href="http://blog.arnaud-k.fr/2009/10/26/5-conseils-pour-developper-avec-jquery/">Lire la suite&#160;&#62;&#62;</a></p>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2009%2F10%2F26%2F5-conseils-pour-developper-avec-jquery%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.arnaud-k.fr%2F2009%2F10%2F26%2F5-conseils-pour-developper-avec-jquery%2F&amp;source=arnaudk&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Pour prolonger la série des tutorials et conseils sur JQuery, je propose un article pour livrer <strong>quelques astuces que j&#8217;ai découvert par moi-même ou glanées sur le web dans le but de mieux coder et d&#8217;optimiser le code JQuery.</strong></p>
<p>En effet, si celui-ci n&#8217;est pas bien optimisé, les perfomances peuvent être détérioriées en fonction des capacités de la machine de visiteur. Le JavaScript est un langage côté client, <strong>il ne faut pas négliger les visiteurs ayant une machine de moindre puissance</strong> !</p>
<p><img class="aligncenter size-full wp-image-1495" title="jQuery_Rock_Star_by_tsinilas" src="http://blog.arnaud-k.fr/wp-content/uploads/2009/10/jQuery_Rock_Star_by_tsinilas.jpg" alt="jQuery_Rock_Star_by_tsinilas" width="500" height="218" /></p>
<h2>1 &#8211; Optimisation des sélecteurs</h2>
<p>Pour commencer, un petit rappel de l&#8217;ordre des sélecteurs de JQuery en fonction de leurs vitesses d&#8217;interprétation (la liste n&#8217;est pas exhaustive) :</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; #monbloc&quot;</span><span style="color: #009900;">&#41;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;p&quot;</span><span style="color: #009900;">&#41;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.laclasse&quot;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Quelques explications s&#8217;imposent.<strong> Le plus rapide reste d&#8217;utiliser directement l&#8217;ID</strong> de ce que l&#8217;on recherche, la fonction étant basé sur la fonction getElementById() de JavaScript. La fonction recherche un objet du DOM unique et s&#8217;arrête dès qu&#8217;elle l&#8217;a trouvé !</p>
<p>Le deuxième sélecteur est basé sur la fonction JavaScript getElementByTagName(). Le nombre d&#8217;éléments à parcourir est moindre par rapport à simplement &laquo;&nbsp;.laclasse&nbsp;&raquo;. <strong>Avec un sélecteur composé simplement d&#8217;un nom de classe, tout le DOM est parcouru !</strong> C&#8217;est ce qu&#8217;il y a de pire !</p>
<p>Dernier petit conseil concernant les sélecteurs, il est toujours mieux de <strong>donner un contexte au sélecteur</strong> si l&#8217;on doit utiliser une classe :</p>
<p>$(&laquo;&nbsp;#monID .maclasse&nbsp;&raquo;) est plus rapide que $(&laquo;&nbsp;.maclasse&nbsp;&raquo;)</p>
<h2>2 &#8211; Utilisation des objets JQuery et des variables JavaScript</h2>
<p>Si tu dois utiliser plusieurs fois le même sélecteur, il faut <strong>utiliser les objets JQuery et enregistrer celui-ci dans une variable</strong>.</p>
<p>Regardons ce bout de code (ceci est bien entendu un exemple à ne pas reproduire !) :</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a#monLien&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// quand on clique sur le lien ayant l'id monLien</span>
     <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#monBloc&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:visible&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// si le bloc &quot;monBloc&quot; est visible</span>
     <span style="color: #009900;">&#123;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#monBloc&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// on l'enroule</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#monBloc&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ferme&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// et on y ajoute la classe &quot;ferme&quot;</span>
     <span style="color: #009900;">&#125;</span>
     e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// pour empecher le navigateur de suivre le lien</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Le sélecteur $(&laquo;&nbsp;#monBloc&nbsp;&raquo;) est utilisé 3 fois. Le DOM est donc parcouru autant de fois pour toujours avoir le même résultat.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a#monLien&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #006600; font-style: italic;">// on affecte à la variable monBloc l'objet JQuery correspondant :</span>
     <span style="color: #003366; font-weight: bold;">var</span> monBloc <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#monBloc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> monBloc.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:visible&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// si le bloc &quot;monBloc&quot; est visible</span>
     <span style="color: #009900;">&#123;</span>
          monBloc.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// on enroule le bloc</span>
          monBloc.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ferme&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// et on y ajoute la classe &quot;ferme&quot;</span>
     <span style="color: #009900;">&#125;</span>
     e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Le simple fait de mettre dans une variable le résultat de $(&laquo;&nbsp;#monBloc&nbsp;&raquo;) permet de n&#8217;<strong>utiliser le sélecteur qu&#8217;une seule fois</strong>. On peut appliquer n&#8217;importe quelle fonction JQuery à la variable monBloc qui contient l&#8217;objet JQuery retourné par le sélecteur !</p>
<h2>3 &#8211; La chainabilité</h2>
<p>L&#8217;un des avantages de JQuery est la possibilité d&#8217;<strong>utiliser la &laquo;&nbsp;chainabilité&nbsp;&raquo; pour écrire son code</strong>. Un exemple vaudra toute les explications.</p>
<p>Reprenons le bout de code précédent :</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a#monLien&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #003366; font-weight: bold;">var</span> monBloc <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#monBloc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> monBloc.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:visible&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#123;</span>
          monBloc.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          monBloc.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ferme&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>On constate que dans la condition if (monBloc&#8230; il y a 2 actions à effectuer sur &laquo;&nbsp;monBloc&nbsp;&raquo;. La chainabilité permet de <strong>raccourcir l&#8217;écriture du code :<br />
</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a#monLien&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #003366; font-weight: bold;">var</span> monBloc <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#monBloc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> monBloc.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:visible&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#123;</span>
          <span style="color: #006600; font-style: italic;">// on enroule le bloc et on y ajoute la classe &quot;ferme&quot; :</span>
          monBloc.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;slow&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ferme&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Par la suite, il est donc possible de faire des choses comme ceci :</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span>
   .<span style="color: #660066;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.class1&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// si un lien à la classe .class1</span>
     .<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// et qu'on clique dessus</span>
       <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Un clique sur le lien .class1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// on alert</span>
       e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// et on empeche le navigateur de suivre le lien</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
   .<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// on retire le sélecteur ajouté avec filter()</span>
   .<span style="color: #660066;">not</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#superId&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// si un lien n'a pas pour id superId</span>
     .<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// et qu'on clique dessus</span>
       $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// on cache le lien</span>
       e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// et on empeche le navigateur de suivre le lien</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
   .<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// et on retire le sélecteur</span>
   ... <span style="color: #006600; font-style: italic;">// on peut continuer trèèèèès longtemps comme ça.</span></pre></div></div>

<p>La chainabilité est possible grâce au fait que <strong>chaque fonction JQuery retourne l&#8217;objet auquel est appliqué la méthode</strong> (ou fonction pour rester dans des termes compréhensibles <img src='http://blog.arnaud-k.fr/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ). La fonction end() permet de &laquo;&nbsp;retirer&nbsp;&raquo; le dernier sélecteur de &laquo;&nbsp;Traversing&nbsp;&raquo; (je vois pas trop le mot à utiliser pour le traduire). Pour voir la liste de ces sélecteurs, <strong>n&#8217;hésite pas à faire un tour sur la <a href="http://docs.jquery.com/Traversing">Doc JQuery</a> !</strong></p>
<h2>4 &#8211; Utiliser le fonctions de toggle</h2>
<p>Une petite astuce toute simple et pourtant très utile. Je ne vois pas très souvent les fonctions de &laquo;&nbsp;toggle&nbsp;&raquo; utilisées&#8230; Or ces fonctions permettent d&#8217;<strong>inverser un état d&#8217;un élément</strong>. Il y en a plusieurs, mais je parle notamment de :</p>
<ul>
<li><strong>toggle()</strong> qui rend visible un élément non visible ou qui cache l&#8217;élément si celui-ci est visible</li>
<li><strong>slideToggle()</strong> qui slideDown (déroule) un élément non visible ou qui slideUp (enroule) l&#8217;élément si celui-ci est visible</li>
<li><strong>toggleClass(&laquo;&nbsp;maClass&nbsp;&raquo;)</strong> ajoute la classe à l&#8217;élément si celui-ci ne l&#8217;a pas ou supprime la classe à l&#8217;élément si celui-ci l&#8217;a déjà</li>
</ul>
<p>Par exemple :</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#monTest&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;:visible&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// si le bloc ayant pour id &quot;monTest&quot; est visible</span>
<span style="color: #009900;">&#123;</span>
     $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#monTest&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// on le cache</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #006600; font-style: italic;">// sinon</span>
<span style="color: #009900;">&#123;</span>
     $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#monTest&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// on le montre</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Tout ce bloc peut être réduit (et donc optimisé) à <strong>1 seule ligne de code :</strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#monTest&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// !!</span></pre></div></div>

<h2>5 &#8211; Utiliser la fonction data() de JQuery</h2>
<p>Chose que j&#8217;ai découverte il y a peu (en refaisant <a title="Portfolio - Arnaud Koncina" href="http://cv.arnaud-k.fr">mon portfolio</a>), il est possible avec JQuery d&#8217;<strong>attacher des données à un élément</strong>.  Cette fonction est très utiliser par certans plugins (comme par exemple <a href="http://jqueryui.com/">JQueryUI</a>). La fonction data() s&#8217;utilise comme ceci :</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#id&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'color'</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#id&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'color'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// On enregistre la couleur du bloc à ce moment</span>
<span style="color: #009900;">&#91;</span>...<span style="color: #009900;">&#93;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#autreId&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'color'</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#id&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'color'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// On affecte la couleur enregistré à un autre élément</span>
<span style="color: #009900;">&#91;</span>...<span style="color: #009900;">&#93;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#id&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeData</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'color'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Et on n'oublie pas de vider le data quand on a fini</span></pre></div></div>

<h3>J&#8217;espère avoir appris au moins une astuce à une personne, comme ça je n&#8217;aurai rien perdu !</h3>
<p style="text-align: right;"><em>[image : <a href="http://tsinilas.deviantart.com/art/jQuery-Rock-Star-118613585">JQuery Rock Star</a> by<small></small><small> <span class="u">tsinilas]</span></small></em></p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 2560px; width: 1px; height: 1px;">http://jqueryui.com/</div>


<p>Pas encore de billet sur le même sujet !</p>]]></content:encoded>
			<wfw:commentRss>http://blog.arnaud-k.fr/2009/10/26/5-conseils-pour-developper-avec-jquery/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

