IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

jQuery Discussion :

fonction toggle() et Jquery


Sujet :

jQuery

  1. #1
    Membre éclairé Avatar de Tillo
    Homme Profil pro
    Webmaster
    Inscrit en
    Octobre 2007
    Messages
    494
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Webmaster
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Octobre 2007
    Messages : 494
    Par défaut fonction toggle() et Jquery
    Bonjour tout le monde

    Jusqu'à aujourd'hui je me contentais d'utiliser Jquery en intégrant tout bêtement des plugins dans mes pages.
    Pour aller un peu plus loin, je suis en train approfondir mes connaissances en Jquery. J'ai trouvé un super bouquin
    et je m'amuse comme un petit fou à créer des choses

    Bref...

    J'ai voulu créé un bouton qui est situé sous un paragraphe: quand on clique sur ce bouton, le paragraphe se cache
    et quand on reclique dessus, il réapparaît. Je sais que ça peut paraître basique à nombre d'entre vous mais il faut
    bien débuter.

    Quoi qu'il en soit, j'ai ce bout de code HTML:

    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    <p id="unediv">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure 
    dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    <p>
     
    <button id="b1">cacher</button>

    et celui-ci en javascript

    Code javascript : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    $("#b1").click(function()
    {
        $("#unediv").toggle(function(){
            $("#b1").text("test 1");
        },function(){
            $("#b1").text("test 2");
        });
    });

    Quand je clique sur le bouton, le paragraphe se cache et le texte sur le bouton devient "test 2" mais quand je clique dessus,
    à nouveau rien ne se passe.

    Si quelqu'un peu éclairer un peu ma lanterne...

    A bientôt,
    Alexis

    PS: c'est rigolo, je viens de me rendre compte que c'est un paragraphe qui a pour identifiant "unediv".

  2. #2
    Rédacteur

    Avatar de danielhagnoul
    Homme Profil pro
    Étudiant perpétuel
    Inscrit en
    Février 2009
    Messages
    6 389
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 74
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant perpétuel
    Secteur : Enseignement

    Informations forums :
    Inscription : Février 2009
    Messages : 6 389
    Billets dans le blog
    125
    Par défaut
    Bonjour

    La méthode "toggle" sert à cacher ou montrer un élément du DOM, voir : http://api.jquery.com/toggle/

    La signature de la méthode "toggle" que vous essayez d'utiliser n'existe plus (http://api.jquery.com/toggle-event/ : deprecated in jQuery 1.8 and removed in jQuery 1.9).

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    <!DOCTYPE html>
    <html lang="fr" dir="ltr">
    <head>
    	<meta charset="utf-8">
    	<meta name="viewport" content="initial-scale=1.0">
    	<meta name="author" content="Daniel Hagnoul">
    	<title>Forum jQuery</title>
    	<script src="http://cdnjs.cloudflare.com/ajax/libs/headjs/0.99/head.min.js"></script>
    	<script>
    		"use strict";
     
    		head.js( 
    			"http://d3js.org/d3.v3.min.js",
    			//"http://danielhagnoul.developpez.com/lib/VerbalExpressions.js",
    			"http://code.jquery.com/jquery-2.0.3.min.js",
    			"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/i18n/jquery-ui-i18n.min.js",
    			"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js",
    			//"http://cdnjs.cloudflare.com/ajax/libs/jquery.caroufredsel/6.2.1/jquery.carouFredSel.packed.js",
    			//"http://danielhagnoul.developpez.com/lib/DataTables/js/jquery.dataTables.js",
    			"http://danielhagnoul.developpez.com/lib/dvjh/d3Base.js", function(){
     
    $( function(){
     
    	var boolOnOff = true;
     
    	$( "#btnOnOff" ).on( "click", function(){
    	    $( "#texteID" ).toggle();
     
    	    if ( boolOnOff ){
    	    	boolOnOff = ! boolOnOff;
    	        $( this ).text( "ON" );
    	    } else {
    	    	boolOnOff = ! boolOnOff;
    	        $( this ).text( "OFF" );
    	    }
    	});
     
    });
     
    $( window ).load( function(){
     
    });
     
    		});
    	</script>
    	<link href='http://fonts.googleapis.com/css?family=Sofia|Ubuntu:400|Kreon'>
    	<link rel="stylesheet" href="http://danielhagnoul.developpez.com/styles/dvjhRemBase.css">
    	<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/sunny/jquery-ui.min.css">
    	<!--<link rel="stylesheet" href="http://danielhagnoul.developpez.com/lib/DataTables/css/jquery.dataTables.css">-->
    	<style>
    		/* TEST -- Nota bene : ici 1 rem est égal à 1 px, voir dvjhRemBase.css */
     
    	</style>
    </head>
    <body>
    	<header>
    		<hgroup>
    			<h1>Forum jQuery</h1>
    			<h2>
    				<a href="">Lien</a>
    			</h2>
    		</hgroup>
    	</header>
    	<section class="conteneur">
     
    <p id="texteID">
    	Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    	Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure 
    	dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    	proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    <p>
     
    <button id="btnOnOff">OFF</button>
     
    	</section>
    	<footer itemscope itemtype="http://danielhagnoul.developpez.com/">
    		<time datetime="2013-09-18T10:04:05.410+02:00" pubdate>2013-09-18T10:04:05.410+02:00</time>
    		<span itemprop="name">Daniel Hagnoul</span>
    		<a href="http://www.developpez.net/forums/u285162/danielhagnoul/" itemprop="url">@danielhagnoul</a>
    		<a href="http://danielhagnoul.developpez.com/" itemprop="url">Mon cahier d’exercices</a>
    		<a href="http://javascript.developpez.com/faq/jquery/" itemprop="url">FAQ</a>
    		<a href="http://javascript.developpez.com/cours/?page=frameworks#jquery" itemprop="url">Tutoriels</a>
    	</footer>
    </body>
    </html><!DOCTYPE html>
    <html lang="fr" dir="ltr">
    <head>
    	<meta charset="utf-8">
    	<meta name="viewport" content="initial-scale=1.0">
    	<meta name="author" content="Daniel Hagnoul">
    	<title>Forum jQuery</title>
    	<script src="http://cdnjs.cloudflare.com/ajax/libs/headjs/0.99/head.min.js"></script>
    	<script>
    		"use strict";
     
    		head.js( 
    			"http://d3js.org/d3.v3.min.js",
    			//"http://danielhagnoul.developpez.com/lib/VerbalExpressions.js",
    			"http://code.jquery.com/jquery-2.0.3.min.js",
    			"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/i18n/jquery-ui-i18n.min.js",
    			"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js",
    			//"http://cdnjs.cloudflare.com/ajax/libs/jquery.caroufredsel/6.2.1/jquery.carouFredSel.packed.js",
    			//"http://danielhagnoul.developpez.com/lib/DataTables/js/jquery.dataTables.js",
    			"http://danielhagnoul.developpez.com/lib/dvjh/d3Base.js", function(){
     
    $( function(){
     
    	var boolOnOff = true;
     
    	$( "#btnOnOff" ).on( "click", function(){
    	    $( "#texteID" ).toggle();
     
    	    if ( boolOnOff ){
    	    	boolOnOff = ! boolOnOff;
    	        $( this ).text( "ON" );
    	    } else {
    	    	boolOnOff = ! boolOnOff;
    	        $( this ).text( "OFF" );
    	    }
    	});
     
    });
     
    $( window ).load( function(){
     
    });
     
    		});
    	</script>
    	<link href='http://fonts.googleapis.com/css?family=Sofia|Ubuntu:400|Kreon'>
    	<link rel="stylesheet" href="http://danielhagnoul.developpez.com/styles/dvjhRemBase.css">
    	<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/sunny/jquery-ui.min.css">
    	<!--<link rel="stylesheet" href="http://danielhagnoul.developpez.com/lib/DataTables/css/jquery.dataTables.css">-->
    	<style>
    		/* TEST -- Nota bene : ici 1 rem est égal à 1 px, voir dvjhRemBase.css */
     
    	</style>
    </head>
    <body>
    	<header>
    		<hgroup>
    			<h1>Forum jQuery</h1>
    			<h2>
    				<a href="">Lien</a>
    			</h2>
    		</hgroup>
    	</header>
    	<section class="conteneur">
     
    <p id="texteID">
    	Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    	Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure 
    	dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    	proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    <p>
     
    <button id="btnOnOff">OFF</button>
     
    	</section>
    	<footer itemscope itemtype="http://danielhagnoul.developpez.com/">
    		<time datetime="2013-09-18T10:04:05.410+02:00" pubdate>2013-09-18T10:04:05.410+02:00</time>
    		<span itemprop="name">Daniel Hagnoul</span>
    		<a href="http://www.developpez.net/forums/u285162/danielhagnoul/" itemprop="url">@danielhagnoul</a>
    		<a href="http://danielhagnoul.developpez.com/" itemprop="url">Mon cahier d’exercices</a>
    		<a href="http://javascript.developpez.com/faq/jquery/" itemprop="url">FAQ</a>
    		<a href="http://javascript.developpez.com/cours/?page=frameworks#jquery" itemprop="url">Tutoriels</a>
    	</footer>
    </body>
    </html>
    De nouvelles versions de jQuery et de jQuery UI sortent régulièrement, il y a eu beaucoup de changements dernièrement. Même si vous utilisez un livre récent, il y a beaucoup de chance que certains passages soient obsolètes.

    Blog

    Sans l'analyse et la conception, la programmation est l'art d'ajouter des bogues à un fichier texte vide.
    (Louis Srygley : Without requirements or design, programming is the art of adding bugs to an empty text file.)

  3. #3
    Membre éclairé Avatar de Tillo
    Homme Profil pro
    Webmaster
    Inscrit en
    Octobre 2007
    Messages
    494
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Webmaster
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Octobre 2007
    Messages : 494
    Par défaut
    Merci beaucoup pour la réponse.

    Je savais que Jquery était mis à jour assez souvent mais j'ignorais qu'il pouvait y avoir de tels changements. Je crois qu'il me reste plus qu'à changer de livre

  4. #4
    Rédacteur

    Avatar de Bovino
    Homme Profil pro
    Développeur Web
    Inscrit en
    Juin 2008
    Messages
    23 647
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2008
    Messages : 23 647
    Billets dans le blog
    20
    Par défaut
    Je crois qu'il me reste plus qu'à changer de livre
    Un livre est destiné à te faire comprendre le fonctionnement d'un langage. S'il est ancien et utilise des méthodes périmées, les principes restent les mêmes, il n'y a plus ensuite qu'à se référer à l'API officielle pour vérifier les méthodes à utiliser.
    Pas de question technique par MP !
    Tout le monde peut participer à developpez.com, vous avez une idée, contactez-moi !
    Mes formations video2brain : La formation complète sur JavaScriptJavaScript et le DOM par la pratiquePHP 5 et MySQL : les fondamentaux
    Mon livre sur jQuery
    Module Firefox / Chrome d'intégration de JSFiddle et CodePen sur le forum

Discussions similaires

  1. Tableau jQuery : Duplication de ligne / Fonction toggle
    Par clem91210 dans le forum jQuery
    Réponses: 6
    Dernier message: 02/07/2014, 23h32
  2. Effet toggle UI jquery
    Par valkiki dans le forum jQuery
    Réponses: 2
    Dernier message: 07/09/2009, 11h16
  3. appliquer un style à la fonction toggle(affichage ou non d'un bloc)
    Par riadhhwajdii dans le forum Général JavaScript
    Réponses: 10
    Dernier message: 04/09/2009, 11h08
  4. Fonction toggle marche po
    Par Lideln dans le forum Général JavaScript
    Réponses: 7
    Dernier message: 26/06/2007, 11h00
  5. [JQUERY] Comment appeler une fonction php avec jquery
    Par popogendarme dans le forum jQuery
    Réponses: 1
    Dernier message: 20/03/2007, 16h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo