<?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>By Myself &#187; jquery</title>
	<atom:link href="http://www.warpdesign.fr/blog/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.warpdesign.fr/blog</link>
	<description>All by Myself</description>
	<lastBuildDate>Sat, 21 Jan 2012 00:18:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Migration jQuery ↔ Prototype</title>
		<link>http://www.warpdesign.fr/blog/2010/08/08/migration-jquery-prototype/</link>
		<comments>http://www.warpdesign.fr/blog/2010/08/08/migration-jquery-prototype/#comments</comments>
		<pubDate>Sun, 08 Aug 2010 14:18:26 +0000</pubDate>
		<dc:creator>leo</dc:creator>
				<category><![CDATA[Techno]]></category>
		<category><![CDATA[Xperiments]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[palm]]></category>
		<category><![CDATA[pixi]]></category>
		<category><![CDATA[pre]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[retrieve]]></category>
		<category><![CDATA[setstyle]]></category>
		<category><![CDATA[store]]></category>
		<category><![CDATA[webkit]]></category>
		<category><![CDATA[webos]]></category>

		<guid isPermaLink="false">http://www.warpdesign.fr/blog/?p=363</guid>
		<description><![CDATA[Ayant récemment porté warpklondike sur le framework jQuery vers le framework Prototype pour la version webOS du jeu, j&#8217;ai pensé que c&#8217;était une bonne idée de partager mon expérience&#8230; Voici donc un petit article présentant les principales différences entre ces deux frameworks.



Introduction
La principale différence entre jQuery et Prototype est que jQuery n&#8217;étend ni le JavaScript, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.warpdesign.fr/blog/images/jquery_prototype.png" alt="jquery prototype"><br />Ayant récemment porté <a href="http://www.warpdesign.fr/entertainment" title="Visiter le site de WarpKlondike">warpklondike</a> sur le framework jQuery vers le framework Prototype pour la version webOS du jeu, j&#8217;ai pensé que c&#8217;était une bonne idée de partager mon expérience&#8230; Voici donc un petit article présentant les principales différences entre ces deux frameworks.
</p>
<p><span id="more-363"></span></p>
<p>
<b>Introduction</b><br />
La principale différence entre jQuery et Prototype est que jQuery n&#8217;étend ni le JavaScript, ni le DOM: jQuery est un simple <i>wrapper</i> autour des éléments du DOM. Les méthodes sont donc disponibles sur les objets/collections jQuery et non sur les éléments du DOM.</p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.maClasse'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #009900; font-style: italic;">// $() nous avons à faire à l'objet jQuery ici</span>
    $<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'selected'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #009900; font-style: italic;">// this est l'objet DOM (non étendu)</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'selected'</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// error: addClass() is not a function</span>
<span style="color: #66cc66;">&#125;</span></code>
</pre>
<p>Prototype, à l&#8217;inverse, étend le DOM (notamment l&#8217;objet Element) avec ses propres méthodes. De même, l&#8217;objet Array se voit étendu. Outre le côté implémentation galère: chaque navigateur change à chaque mise à jour, implémentant de nouvelles méthodes: que se passe-t&#8217;il lorsque le navigateur implémente la méthode document.getElementByClassName() ensuite étendue par le framework ? Une grosse <b>collision</b> <img src='http://www.warpdesign.fr/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Une fois le framework fixé, il faut aussi fixer tout le code utilisant cette méthode&#8230;</p>
<pre>
<code><span style="color: #003366; font-weight: bold;">var</span> monDiv = <span style="color: #003366; font-weight: bold;">new</span> Element<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;
monDiv.<span style="color: #006600;">addClassName</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'selected'</span><span style="color: #66cc66;">&#41;</span>;  <span style="color: #009900; font-style: italic;">// fonctionne, puisque l'objet Element a été étendu par Prototype</span></code>
</pre>
<p>Il semblerait donc que l&#8217;approche utilisée par jQuery soit bien meilleure. Il est à noter que la version 2.0 de Prototype n&#8217;étendra d&#8217;ailleurs pas le DOM comme cela avait été le cas jusqu&#8217;à maintenant: <a href="http://ajaxian.com/archives/prototype-2-0-will-not-extend-the-dom" target="_blank">Ajaxian: prototype 2.0 will not extend the dom</a>.<br />
<b>1. le sélecteur $</b><br />
<b>$</b> est surement la première chose que l&#8217;on découvre en se lançant dans jQuery, et la fonctionne existe aussi dans Prototype, mais les deux fonctionnent différemment. Dans jQuery, <b>$</b> fait office de sélecteur universel:</p>
<pre>
<code><span style="color: #003366; font-weight: bold;">var</span> $maCollection = $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.maClass'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> $mesInput = $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'form input'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> $monSubmit = $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#monSubmit'</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>Avec Prototype, <b>$</b> est en fait un raccourci vers <i>document.getElementById()</i>. Pour obtenir un sélecteur universel comme sur jQuery il faudra utiliser le <b>$$</b>:<br />
deviendra:</p>
<pre>
<code><span style="color: #003366; font-weight: bold;">var</span> $maCollection = $$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.maClass'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> $mesInput = $$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'form input'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> $monSubmit = $$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#monSubmit'</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p><b>Note:</b> l&#8217;appel d&#8217;une méthode sur une collection d&#8217;éléments jQuery appellera automatiquement la méthode sur chaque élément. Par exemple $(&#8217;.maClasse&#8217;).addClass(&#8217;selected&#8217;) appliquera la classe <i>selected</i> à chaque élément. A l&#8217;inverse, Prototype étendant le DOM, les méthodes sont accessibles sur chaque élément et non pas sur la collection d&#8217;éléments: $$(&#8217;.maClass&#8217;).addClassName(&#8217;selected&#8217;) ne fonctionnera pas. Il faudrait parcourir la collection avec each().</p>
<p>
<b>2. document.ready()</b><br />
Le JavaScript a accès au DOM avant même qu&#8217;il soit finit d&#8217;être chargé. Pour être sûr qu&#8217;il le soit, et que nos modifications puissent donc fonctionner, jQuery fournit la méthode $(document).ready(), automatiquement appelée lorsque le DOM est prêt:</p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">ready</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
   <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'prêt !'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>L&#8217;équivalent Prototype (1.6) serait:</p>
<pre>
<code>document.<span style="color: #006600;">observe</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'contentloaded'</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
   <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'prêt !'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>
<b>3. itération</b><br />
jQuery utilise la méthode each() pour itérer sur une collection d&#8217;éléments. A l&#8217;intérieur de la fonction each, this correspond à l&#8217;objet DOM (non étendu), et $(this) permet d&#8217;accéder à l&#8217;objet jQuery. Enfin, each passe l&#8217;index de l&#8217;élément en cours à la fonction callback (optionnel):</p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.maClasse'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
   $<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'selected'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>Prototype fournit une méthode each() équivalente, à la différence prêt que le this n&#8217;est pas assigné, comme par magie dans jQuery (beaucoup de choses se font par magie dans jQuery <img src='http://www.warpdesign.fr/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ) à l&#8217;objet courant. Dans Prototype la fonction callback se voit passer comme paramètres l&#8217;élément courant, et l&#8217;index:</p>
<pre>
<code>$$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.maClasse'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>elt, i<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
   elt.<span style="color: #006600;">addClassName</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'selected'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p></p>
<p><b>4. modification de contenu</b><br />
Pour modifier le contenu d&#8217;un élément avec jQuery, il existe principalement deux manières. On veut écraser tout le contenu d&#8217;un élément:</p>
<pre>
<code><span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;monParagraphe&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   Ancien contenu.
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></code>
</pre>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#monParagraphe'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'nouveau contenu :)'</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>Ce qui donnera:</p>
<pre>
<code><span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;monParagraphe&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   nouveau contenu :)
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></code>
</pre>
<p>
Le code Prototype correspondant serait:</p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'monParagraphe'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">update</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'nouveau contenu :)'</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>
Pour ajouter un élément, ou du texte à autre élément, jQuery fournit les méthodes: <b>append()</b> et <b>prepend()</b> qui rajoutent respectivement du contenu en fin, et en début d&#8217;élément:</p>
<pre>
<code><span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;monParagraphe&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   Ancien contenu.
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></code>
</pre>
<p></p>
<pre>
<code><span style="color: #003366; font-weight: bold;">var</span> $span1 = $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;span/&gt;'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'avant'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> $span2 = $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;span/&gt;'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'apres'</span><span style="color: #66cc66;">&#41;</span>;
$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#monParagraphe'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">prepend</span><span style="color: #66cc66;">&#40;</span>$span1<span style="color: #66cc66;">&#41;</span>;
$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#monParagraphe'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">append</span><span style="color: #66cc66;">&#40;</span>$span2<span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>Ce qui donnera:</p>
<pre>
<code><span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;monParagraphe&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span&gt;</span></a></span>avant<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span>
   nouveau contenu :)
   <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span&gt;</span></a></span>apres<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></code>
</pre>
<p>
Prototype fournit une méthode &laquo;&nbsp;tout-en-un&nbsp;&raquo; <b>insert</b> qui permet de rajouter du contenu en début et fin d&#8217;élément en un seul appel. Le code Prototype correspondant serait:</p>
<pre>
<code><span style="color: #003366; font-weight: bold;">var</span> $span1 = <span style="color: #003366; font-weight: bold;">new</span> Element<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">update</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'avant'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> $span2 = <span style="color: #003366; font-weight: bold;">new</span> Element<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">update</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'apres'</span><span style="color: #66cc66;">&#41;</span>;
$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'monParagraphe'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">insert</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>
   top: $span1,
   bottom: $span2
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>
<b>5. Modification CSS</b><br />
Pour modifier l&#8217;aspect d&#8217;un élément à l&#8217;aide des CSS, rien de plus simple avec jQuery: la méthode <b>css()</b> permet de modifier un ou plusieurs attributs CSS:</p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#monParagraphe'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">css</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>
  border: <span style="color: #3366CC;">'1px solid red'</span>,
  <span style="color: #3366CC;">'background-color'</span>: <span style="color: #3366CC;">'green'</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>Ce qui donnera:</p>
<pre>
<code><span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;monParagraphe&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;border:1px solid red; background-color:green;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span&gt;</span></a></span>avant<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span>
   nouveau contenu :)
   <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span&gt;</span></a></span>apres<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></code>
</pre>
<p><b>Note:</b> La méthode CSS reconnait à la fois les noms d&#8217;attributs CSS et leur variante Camel Case. Pour modifier le &laquo;&nbsp;background-color&nbsp;&raquo;, on aurait donc aussi pu utiliser: backgroundColor: &#8216;green&#8217;.<br />
<br />
Prototype fournit la méthode équivalente <b>setStyle</b>:</p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'monParagraphe'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">setStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>
  border: <span style="color: #3366CC;">'1px solid red'</span>,
  backgroundColor: <span style="color: #3366CC;">'green'</span>
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p><b>Note:</b> Prototype ne reconnait que du Camel Case. Il n&#8217;est donc pas possible de modifier l&#8217;attribut background-color en utilisant: &#8216;background-color&#8217;: &#8216;green&#8217; et il faut impérativement utiliser du Camel Case comme dans l&#8217;exemple précédent.<br />
<br />
<b>6. Cacher/Afficher des éléments</b><br />
Pour cacher ou afficher un élément avec jQuery, rien de plus simple:</p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#monParagraphe'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">hide</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>aura pour effet de cacher le paragraphe:</p>
<pre>
<code><span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;monParagraphe&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display:none;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  Mon Paragraphe
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></code>
</pre>
<p>Pour réafficher un paragraphe caché (par CSS ou bien par jQuery), il suffit de faire appel à la méthode <b>show()</b>:</p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#monParagraphe'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>
Prototype fournit deux méthodes équivalentes: <b>show</b> et <b>hide</b> mais <b>attention</b> ! Prototype <b>n&#8217;est pas capable d&#8217;afficher un élément qui aurait été caché au niveau CSS</b> (ayant la propriété display settée à <i>none</i>). Exemple:</p>
<pre>
<code><span style="color: #cc00cc;">#monParagraphe</span><span style="color: #66cc66;">&#123;</span>
  display<span style="color: #3333ff;">:none</span>;
<span style="color: #66cc66;">&#125;</span></code>
</pre>
<p>L&#8217;appel à show() <b>n&#8217;aura aucun effet</b> dans ce cas:</p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#monParagraphe'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>Il sera nécessaire dans ce cas de modifier directement la propriété CSS display en écrivant par exemple:</p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#monParagraphe'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">style</span>.<span style="color: #006600;">display</span> = <span style="color: #3366CC;">'block'</span>;</code>
</pre>
<p>
<b>7. Pour aller plus loin</b><br />
jQuery permet d&#8217;assigner des données à chaque élément du DOM à l&#8217;aide de la méthode <b>data</b>:</p>
<pre>
<code><span style="color: #003366; font-weight: bold;">var</span> mesDonnees = <span style="color: #66cc66;">&#123;</span>test: <span style="color: #3366CC;">'yep !'</span><span style="color: #66cc66;">&#125;</span>;
$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#monParagraphe'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">data</span><span style="color: #66cc66;">&#40;</span>mesDonnees<span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>Pour récupérer ces mêmes données, il suffit alors d&#8217;appeler <b>data</b> sans paramètre:</p>
<pre>
<code><span style="color: #003366; font-weight: bold;">var</span> mesDonnees = $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#monParagraphe'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">data</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// { test: 'yop !' }</span></code>
</pre>
<p>
Là encore, Prototype fournit les méthodes <b>store</b> et <b>retrieve</b> équivalentes:</p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'monParagraphe'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">store</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'test'</span>, <span style="color: #3366CC;">'yep !'</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">// { test: 'yop !' }</span>
<span style="color: #003366; font-weight: bold;">var</span> test = $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'monParagraphe'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">retrieve</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'test'</span><span style="color: #66cc66;">&#41;</span>;  <span style="color: #009900; font-style: italic;">// test == 'yop !'</span></code>
</pre>
<p>
<b>Quelques liens</b><br />
Pour finir, et pour en savoir plus, voici quelques liens utiles:</p>
<ul>
<li>Documentation officielle de jQuery: <a target="_blank" href="http://api.jquery.com/" title="Documentation jQuery">api.jquery.com</a></li>
<li>Documentation officielle de Prototype: <a target="_blank" href="http://api.prototypejs.org/" title="Documentation jQuery">http://api.prototypejs.org/</a></li>
</ul>
<p><img src="http://www.warpdesign.fr/blog/images/jaquettes/sleepysunFeverArt.jpg" style="float:left;margin-right:14px;" alt="Fever Art" /><a href="http://www.myspace.com/sleepysun" target="_blank">SleepySun</a> &nbsp;[ Rock spychiadélique ]<br />
<b>Ambiance:</b> Rock Psychiadélique<br />
<b>Albums:</b> Fever Art<br />
<b>A écouter:</b> Open Eyes<br />
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.warpdesign.fr/blog/2010/08/08/migration-jquery-prototype/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery 1.3.1: toujours mieux&#8230;</title>
		<link>http://www.warpdesign.fr/blog/2009/01/28/jquery-131-toujours-mieux/</link>
		<comments>http://www.warpdesign.fr/blog/2009/01/28/jquery-131-toujours-mieux/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 16:16:12 +0000</pubDate>
		<dc:creator>leo</dc:creator>
				<category><![CDATA[Techno]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery1.3]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[nouveautes]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[selector]]></category>
		<category><![CDATA[sizzle]]></category>

		<guid isPermaLink="false">http://www.warpdesign.fr/blog/?p=203</guid>
		<description><![CDATA[jQuery est passé en version 1.3 puis rapidement en version 1.3.1. Cette version apporte le lot habituel de changements (optimisations, nouvelles fonctionnalités, réécritures, etc&#8230;) mais aussi, et surtout, casse la compatibilité avec les versions précédentes. Comme je me suis cassé les dents dessus, sans le savoir, j&#8217;ai pensé qu&#8217;il serait bon de faire un petit [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.warpdesign.fr/blog/images/jquery.png" alt="jquery" style="float: left; margin-right: 10px;"><b>jQuery</b> est passé en version 1.3 puis rapidement en version 1.3.1. Cette version apporte le lot habituel de changements (optimisations, nouvelles fonctionnalités, réécritures, etc&#8230;) mais aussi, et surtout, casse la compatibilité avec les versions précédentes. Comme je me suis cassé les dents dessus, sans le savoir, j&#8217;ai pensé qu&#8217;il serait bon de faire un <b>petit tour de quelques unes des nouveautés et incompatibilités introduites dans cette version</b>&#8230;
</p>
<p><span id="more-203"></span></p>
<p>
<b>Nouveautés</b><br />
Comme à chaque version majeure (et on peut se demander comment ils font), les développeurs de ce framework ont de nouveau augmenté les performances de jQuery:<br />
<img src="http://www.warpdesign.fr/blog/images/jquery_chart.jpg" alt="performances de jquery" style="float:left;margin:16px auto;" /></p>
<ul style="clear:left;">
<li>Réécriture de l&#8217;injection HTML: <b>6x</b> plus rapide!</li>
<li>Utilisation d&#8217;un nouveau sélecteur, <b>plus rapide:</b> <i>sizzle</i></li>
<li>Ajout des live events dans le core de jQuery</li>
<li>Nouvel objet Event</li>
<li>Réécriture de la méthode offset(), beaucoup plus rapide</li>
<li>Et bien plus&#8230;</li>
</ul>
<p><b>Incompatibilités</b><br />
Malheureusement, certaines fonctionnalités dépréciées depuis un moment ont été retirées de cette version. Les modifications à effectuer pour rendre son code compatible sont plutôt mineures, quand on sait où chercher <img src='http://www.warpdesign.fr/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<br />
<b>1. suppression de @ dans [@attr]</b><br />
Le plus gros changement est la suppression de @. Une sélection comme:</p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'input[@name=toto]'</span><span style="color: #66cc66;">&#41;</span>...</code>
</pre>
<p>deviendra:</p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'input[name=toto]'</span><span style="color: #66cc66;">&#41;</span>...</code>
</pre>
<p>
<b>2. changements dans la méthode ready()</b><br />
Auparavant la méthode attendait le chargement de toutes les feuilles de styles. Comme ce n&#8217;est plus le cas, tous les fichiers CSS doivent être inclus avant les scripts dans la page.<br />
<br />
<b>3. Ordre des sélecteurs &laquo;&nbsp;a, b, c&nbsp;&raquo;</b><br />
L&#8217;ordre des sélecteurs peut varier. Les navigateurs supportant querySelectorAll (Safari, Firefox 3.1+, Opera 10+, IE8+) retourneront les éléments dans l&#8217;ordre du document alors que les autres navigateurs les retourneront (pour l&#8217;instant) dans l&#8217;ordre spécifié. Dans une prochaine version 1.3.x tous les sélecteurs séparés par des virgules seront retournés dans l&#8217;ordre du document.<br />
Concrètement, cela signifie que dans l&#8217;exemple suivant:<br />
<b>DOM</b></p>
<pre>
<code><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;c&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	Div c
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;b&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	Div b
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;a&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	Div a
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></code>
</pre>
<p>
<b>Code</b></p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">ready</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#a, #b, #c'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">each</span><span style="color: #66cc66;">&#40;</span>
      <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
         <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span> $<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">attr</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>; 
      <span style="color: #66cc66;">&#125;</span>
   <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>Les navigateurs supportant querySelectorAll (cf. au dessus), renverront les éléments dans l&#8217;ordre du DOM, à savoir:</p>
<pre>
<code>c b a</code>
</pre>
<p>Alors que les autres renverront les éléments dans l&#8217;ordre spécifié dans le sélecteur, c&#8217;est à dire:</p>
<pre>
<code>a b c</code>
</pre>
<p>Attention à ne pas se baser sur cet ordre donc !<br />
<br />
<b>Nouveautés</b><br />
Cette nouvelle version de jQuery vient comme d&#8217;habitude avec son lot de nouveautés bien pratiques, parmi lesquelles:<br />
<br />
<b>1. nouvelle méthode closest( expr )</b><br />
Cette méthode renvoie le plus proche parent correspondant à l&#8217;expression, l&#8217;élément de départ compris. Exemple:<br />
<br />
<b>DOM</b></p>
<pre>
<code><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;superParent&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;main&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
     <span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div&gt;</span></a></span>
        <span style="color: #009900;"><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">&lt;ul</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;maListe&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">&lt;li</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;15&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
				<span style="color: #009900;"><a href="http://december.com/html/4/element/a.html"><span style="color: #000000; font-weight: bold;">&lt;a&gt;</span></a></span>clique !<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span></code>
</pre>
<p>
<b>Code</b></p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">click</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">closest</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">attr</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">closest</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div.superParent'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">attr</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>L&#8217;exécution du code ci-dessus donnera le résultat suivant:</p>
<pre>
<code>15
main</code>
</pre>
<p>
<b>2. live events</b><br />
Auparavant, les événements n&#8217;étaient pas mis à jour en temps réel sans passer par un plugin comme <a href="http://docs.jquery.com/Plugins/livequery" target="_blank">livequery</a> et un appel comme celui là:</p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #66cc66;">&#41;</span> 
    .<span style="color: #006600;">livequery</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'click'</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
        <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'clique !'</span><span style="color: #66cc66;">&#41;</span>; 
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>; 
    <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p>sera tout simplement remplacé par:</p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #66cc66;">&#41;</span> 
    .<span style="color: #006600;">live</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'click'</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
        <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'clique !'</span><span style="color: #66cc66;">&#41;</span>; 
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>; 
    <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</code>
</pre>
<p><b>3. La méthode is() et selecteur not() supportent maintenant des expressions complexes, comme:</b></p>
<pre>
<code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'a:not(.selected, .highlighted)'</span><span style="color: #66cc66;">&#41;</span>...</code>
</pre>
<p>
<b>Quelques liens</b><br />
Pour finir, et pour en savoir plus, voici quelques liens sur jQuery:</p>
<ul>
<li>changelog complet: <a target="_blank" href="http://docs.jquery.com/Release:jQuery_1.3" title="changelog jQuery 1.3">changelog jQuery 1.3</a></li>
<li>Introduction à <a target="_blank" href="/blog/2007/06/11/jquery/" title="Introduction à jQuery">jQuery</a></li>
<li>Documentation officielle: <a target="_blank" href="http://docs.jquery.com/" title="Documentation jQuery">docs.jquery.com</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.warpdesign.fr/blog/2009/01/28/jquery-131-toujours-mieux/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>jQuery</title>
		<link>http://www.warpdesign.fr/blog/2007/06/11/jquery/</link>
		<comments>http://www.warpdesign.fr/blog/2007/06/11/jquery/#comments</comments>
		<pubDate>Mon, 11 Jun 2007 21:50:28 +0000</pubDate>
		<dc:creator>leo</dc:creator>
				<category><![CDATA[Techno]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[motools]]></category>

		<guid isPermaLink="false">http://www.warpdesign.fr/blog/?p=26</guid>
		<description><![CDATA[
Si vous êtes un petit au courant des dernières technologies du web, vous avez sans doute entendu parlé d&#8217;Ajax. Tout le monde en a fait tout un foin, mais Ajax existe depuis bien longtemps et a été pour la première fois introduit par un certain Internet Explorer (ça alors!  ). Et depuis, chaque navigateur [...]]]></description>
			<content:encoded><![CDATA[<p>
Si vous êtes un petit au courant des dernières technologies du web, vous avez sans doute entendu parlé d&#8217;Ajax. Tout le monde en a fait tout un foin, mais Ajax existe depuis bien longtemps et a été pour la première fois introduit par un certain Internet Explorer (ça alors! <img src='http://www.warpdesign.fr/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ). Et depuis, chaque navigateur a plus ou moins imposé sa propre API. Donc pour nous développeurs, c&#8217;est la galère!<br />
<br />
<b>Mais heureusement jQuery est né!</b></p>
<p><span id="more-26"></span><br />
Si vous êtes ici c&#8217;est que vous ne connaissez pas jQuery et que vous voulez en savoir plus&#8230; Alors fini le blabla <img src='http://www.warpdesign.fr/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Non, jQuery n&#8217;est pas un nouvel applet Java, mais bel et bien un framework Javascript qui permet, entre autres de simplifier l&#8217;utilisation de l&#8217;ajax, mais aussi de manipuler le DOM de votre document, et tout ceci, très, très simplement!<br />
Comme un exemple vaut souvent mieux qu&#8217;un long discours, et que de toutes façons je suis mauvais pour les discours, je vais vous démontrer sa simplicité et sa puissance en quelques exemples&#8230;<br />
<br />
Commençons par l&#8217;exemple le plus simple. Admettons que notre document contienne un élément d&#8217;id &laquo;&nbsp;truc&nbsp;&raquo;, et que nous souhaitions injecter une chaîne de caractères &laquo;&nbsp;bonjour !&nbsp;&raquo; à l&#8217;intérieur:</p>
<pre><code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#truc&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;bonjour !&quot;</span><span style="color: #66cc66;">&#41;</span>;</code></pre>
<p><b>Au niveau du DOM, avant:</b></p>
<pre><code><span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;truc&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></code></pre>
<p><b>&#8230;Après:</b></p>
<pre><code><span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;truc&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>bonjour !<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></code></pre>
<p>Alors, deux choses. L&#8217;accesseur &laquo;&nbsp;$&nbsp;&raquo; permet d&#8217;accéder à un objet jQuery. Ici, d&#8217;identifiant &laquo;&nbsp;truc&nbsp;&raquo;. $(&nbsp;&raquo;#truc&nbsp;&raquo;) nous renvoie donc un objet (au sens javascript, mais aussi jQuery) qui possède de nombreuses méthodes. La méthode html() permet de modifier le contenu innerHTML de l&#8217;objet avec le contenu passé en paramètre. Simple, non ? Maintenant admettons que nous souhaitions modifier la classe utilisée par cet élément. Rien de plus simple en jQuery:</p>
<pre><code>$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#truc&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;ma_classe&quot;</span><span style="color: #66cc66;">&#41;</span>;</code></pre>
<p><b>Au niveau du DOM, avant:</b></p>
<pre><code><span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;truc&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>bonjour !<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></code></pre>
<p><b>&#8230;Après:</b></p>
<pre><code><span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;truc&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ma_classe&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>bonjour !<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></code></pre>
<p>Comme vous pouvez le voir notre paragraphe possède maintenant la classe &laquo;&nbsp;ma_classe&nbsp;&raquo;. Simple, non ? Maintenant voyons un peu comment fonctionne l&#8217;Ajax&#8230; Admettons que nous ayons un script &laquo;&nbsp;heure.php&nbsp;&raquo; qui nous renvoie l&#8217;heure, sous forme de chaîne de caractères&#8230; Et que nous souhaitions modifier le contenu de notre paragraphe d&#8217;id &laquo;&nbsp;truc&nbsp;&raquo; avec le résultat de l&#8217;appel Ajax de ce script&#8230; Rien de plus simple avec jQuery:</p>
<pre><code><span style="color: #003366; font-weight: bold;">function</span> set_time<span style="color: #66cc66;">&#40;</span>msg<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#truc&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span>msg<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
$.<span style="color: #006600;">post</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;http://mon_site/heure.php&quot;</span>, set_time<span style="color: #66cc66;">&#41;</span>;</code></pre>
<p><b>Au niveau du DOM, avant:</b></p>
<pre><code><span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;truc&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></code></pre>
<p><b>&#8230;Après:</b></p>
<pre><code><span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;truc&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Mardi 12 Juin - 18:36:45<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span></code></pre>
<p>Alors, qu&#8217;est-ce que l&#8217;on voit ici ? Nous définissons tout d&#8217;abord une fonction javascript set_time() qui prend en paramètre un message msg. C&#8217;est en fait la fonction <i>callback</i> qui sera appelée lorsque la requête AJax aura été effectuée. Le paramètre contiendra le message renvoyé par le serveur. Cette fonction, modifie le contenu <i>innerHTML</i> du paragraphe d&#8217;id &laquo;&nbsp;truc&nbsp;&raquo;, comme nous l&#8217;avons vu précédemment: jusque là rien de nouveau. La ligne suivante fait appel à une méthode de jQuery (d&#8217;où le $.), &laquo;&nbsp;post&nbsp;&raquo; qui effectue une requête asynchrone vers le serveur http://mon_site sur le script heure.php. Lorsque le serveur renvoie son résultat, la fonction set_time() est automatiquement appelée avec en paramètre le résultat de la requête Ajax.<br />
Ici on a choisit de passer le nom d&#8217;une fonction javascript comme second paramètre de la méthode post(), mais il tout à fait possible de définir la fonction de manière <i>inline</i>:</p>
<pre><code>$.<span style="color: #006600;">post</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;http://mon_site/heure.php&quot;</span>, <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span>msg<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#truc&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span>msg<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</code></pre>
<p>
<b>Note:</b> le code devient vite illisible et difficile à maintenir lorsque l&#8217;on abuse d&#8217;appels inline!<br />
<br />
<b>Les évènements</b><br />
Vous avez sans doute déjà <i>bindé</i> (lié) un évènement (par exemple <i>onClick</i>) à une fonction en javascript, par exemple avec la fonction onClick().<br />
Cette méthode permettant de lier un évènement à un objet a plusieurs inconvénients. Le premier est que le code se retrouve mélangé avec le HTML. Le deuxième est que le code est statique: il est définit une bonne fois pour toutes lors de la création/génération de la page.<br />
Et bien avec jQuery, ces deux inconvénients disparaissent puisqu&#8217;il est maintenant possible de <i>binder</i> n&#8217;importe quel évènement Javascript à n&#8217;importe quel élément de notre DOM. Ainsi, en reprenant l&#8217;exemple précédent, et en rajoutant un bouton nous obtenons le code suivant:</p>
<pre><code><span style="color: #003366; font-weight: bold;">function</span> set_time<span style="color: #66cc66;">&#40;</span>msg<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#truc&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span>msg<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> envoi_requete<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    $.<span style="color: #006600;">post</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;http://mon_site/heure.php&quot;</span>, set_time<span style="color: #66cc66;">&#41;</span>; 
<span style="color: #66cc66;">&#125;</span>
&nbsp;
$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;#mon_bouton&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">click</span><span style="color: #66cc66;">&#40;</span> envoi_requete <span style="color: #66cc66;">&#41;</span>;</code></pre>
<p>Aussi simplement que l&#8217;on modifie le contenu <i>innerHTML</i> d&#8217;un objet avec la méthode html(), il est possible de binder un évènement à <b>n&#8217;importe quel</b> objet ! La méthode click() permet de binder, comme vous l&#8217;aurez deviné, l&#8217;évènement &laquo;&nbsp;clique&nbsp;&raquo; de la souris à n&#8217;importe quel objet. Le code précédent va donc appeler la fonction envoi_requete() dès que l&#8217;utilisateur cliquera sur le bouton d&#8217;id &laquo;&nbsp;mon_bouton&nbsp;&raquo;. Cette fonction déclenchera la requête Ajax. Une fois cette requête terminée, le résultat sera injecté dans le innerHTML de l&#8217;objet d&#8217;id &laquo;&nbsp;truc&nbsp;&raquo;. A noter qu&#8217;encore une fois, il est ici tout à fait possible de définir de manière <i>inline</i> la fonction appelée lorsque l&#8217;évènement clique est détecté.<br />
<br />
<b>Pour finir&#8230;</b><br />
Cette petite introduction à jQuery touche à sa fin. Non pas que je n&#8217;ai plus rien à dire: je pourrai parler des possibilités offertes par cette librairie pendant des heures, mais il commence à se faire tard, et le but de cet article était seulement de donner un petit aperçu de cette librairie et des différents domaines qu&#8217;elle touche. Avant de finir je vais cependant introduire une dernière fonction très utile et importante, la méthode</p>
<pre><code>$<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">ready</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></code></pre>
<p>Elle est appelée une fois le DOM de la page complètement chargé. Lorsque vous utilisez jQuery, il est <b>obligatoire</b> de binder vos différents évènements dans cette fonction, de manière à être sûr que tout fonctionne correctement:</p>
<pre><code>$<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">ready</span><span style="color: #66cc66;">&#40;</span>
<span style="color: #003366; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
   <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'Le DOM est prêt :)'</span><span style="color: #66cc66;">&#41;</span>; 
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</code></pre>
<p>
<b>Quelques liens</b></p>
<ul>
<li><a href="http://www.jquery.com">jQuery.com</a>: Site Officiel</li>
<li><a href="http://http://www.visualjquery.com">VisualjQuery</a>: Documentation très bien faite (malheureusement plus mise à jour depuis longtemps)</li>
</ul>
<p>
Ah, et j&#8217;oubliais: merci à lolo de m&#8217;avoir fait découvrir cette petite perle <img src='http://www.warpdesign.fr/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<br />
Et j&#8217;oubliais aussi: rien à voir avec jQuery, mais voilà le petit extrait musical du moment&#8230;<b>Until the Morning &#8211; Thievery Corporation</b></p>
<div align="center"></div></p>
]]></content:encoded>
			<wfw:commentRss>http://www.warpdesign.fr/blog/2007/06/11/jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

