$ non défini en utilisant jQuery dans WordPress
3 réponses
- votes
-
- 2010-10-14
Vouspouvezenvelopper votrejavascript dans unefonction auto-appelante,puis luipasser
jQuery
comme argument,en utilisant$
commenom de variable locale.Parexemple:(function($) { $(document).ready(function(){ $("ul.vimeo_desc_feed li a").click(function(){ alert($(this).attr('href')); return false; }) }); }(jQuery));
devraitfonctionner commeprévu.
Sije me souviensbien de la versionfourniepar WP dejQuery (celle que vous obtenez si vous
wp_enqueue_script('jquery')
)met immédiatementjQueryen non-conflit,provoquant$
indéfini.You can wrap your javascript inside a self-invoking function, then pass
jQuery
as an argument to it, using$
as the local variable name. For example:(function($) { $(document).ready(function(){ $("ul.vimeo_desc_feed li a").click(function(){ alert($(this).attr('href')); return false; }) }); }(jQuery));
should work as intended.
If I remember correctly the WP-supplied version of jQuery (the one you get if you
wp_enqueue_script('jquery')
) puts jQuery in no-conflict immediately, causing$
to be undefined.-
aah,je vois.J'avais l'habitude de l'ajouter à lamain,ce quiexpliquepourquoije n'aipas rencontré ceproblème.aah, I see. I used to add it by hand, which explains why I have not come across this issue.
- 0
- 2010-10-14
- Mild Fuzz
-
Merci,j'utilisais leformat alternatif avecjQuery au début au lieu de lafin ..maisje nepouvaispas comprendre comment renvoyer une valeur,avec ceformat,je viens d'ajouter return avant lafonction auto-appelante,et celafonctionne.Thank you, I was using the alternate format with jQuery at the beginning instead of end.. but couldn't figure out how to return a value, with this format I just added return before the self-invoking function, and it works.
- 0
- 2015-01-15
- eselk
-
Réponsetrès utile.Very useful answer.
- 0
- 2017-11-01
- MarkSkayff
-
- 2010-10-14
Vous y êtespresque!
jQuery(document).ready(function($){ $("ul.vimeo_desc_feed li a").click(function(){ alert($(this).attr('href')); return false; }) });
Vous devezpasser une référence àjQueryen tant quefonction
$
dans votreméthode ou celane fonctionnerapas.Si vousplacez simplement un $ dans lepremier appel defunction()
commeje l'aifait ci-dessus,les chosesfonctionneronttrèsbien.You're almost there!
jQuery(document).ready(function($){ $("ul.vimeo_desc_feed li a").click(function(){ alert($(this).attr('href')); return false; }) });
You have to pass a reference to jQuery as the
$
function into your method or it won't work. If you just place a $ inside the firstfunction()
call as I did above, things will be working just fine.-
+1: C'estplus lisible que demettre `jQuery` à lafin.+1: That is more readable than putting `jQuery`at the end.
- 4
- 2010-10-14
- fuxia
-
...mais cen'estpas lamanière standard defaire unefonction anonyme.http://forum.jquery.com/topic/jquery-anonymous-function-calls...but it isn't the standard way of doing an anonymous function. http://forum.jquery.com/topic/jquery-anonymous-function-calls
- 1
- 2010-10-14
- BryanH
-
Ouiet non.Ils sonttous deux considérés comme desmoyens «standard» de lefaire.On crée une classe singleton qui a «$» défini localement.L'autre définit simplement ungestionnairepour l'événement `ready` du documentet passe l'objetjQuery dans legestionnaireen tant que` $ `.Si vousessayez de vous connecter à l'événement «ready»,la deuxièmeméthodeestplus largement utilisée.Si vous avezbesoin dejQuery dans un autrebut (pour vous connecter à `$ .browser`parexemple),vous utiliserez une classe singleton.Yes and no. They're both considered "standard" ways of doing it. One creates a singleton class that has `$` defined locally. The other just defines a handler for the document's `ready` event and passes the jQuery object into the handler as `$`. If you're trying to hook on to the `ready` event, the second method is more widely used. If you need jQuery for any other purpose (to hook on to `$.browser` for example), you'd use a singleton class.
- 5
- 2010-10-15
- EAMann
-
+1pourjQuery (document) .ready (function ($) {... Pourplus d'informations surjqueryet WordPress,vouspouvez également lire surmonpost: http://wpengineer.com/2028/small-tips-using-wordpress-and-jquery/.+1 for jQuery(document).ready(function($){... mor infos about jquery and WordPress can you also read on my post: http://wpengineer.com/2028/small-tips-using-wordpress-and-jquery/ .
- 0
- 2010-10-15
- bueltge
-
- 2013-10-09
Passer unefonction àjQueryest un raccourcipour
$(document).ready(...)
puisen plaçant$
commepremierparamètre de votre callback,vous créezun aliaspourjQuery dans ce rappel:jQuery(function($) { $("ul.vimeo_desc_feed li a").click(function(){ alert($(this).attr('href')); return false; }); });
Vouspouvez consulter la documentation de ce ici .
Passing a function to jQuery is shorthand for
$(document).ready(...)
then by placing$
as the first parameter of your callback, you create an alias for jQuery within that callback:jQuery(function($) { $("ul.vimeo_desc_feed li a").click(function(){ alert($(this).attr('href')); return false; }); });
You can see the documentation for this here.
Je sais quejQueryest chargé,carje peux changer le
$
pour 'jQuery'et tout se comporte commeprévu,mais ce sera un script compliqué sije nepeuxpas résoudre ceproblèmeCe script:
Produit l'erreur
$ is not a function
Ce script:
fonctionnetrèsbien.