Précédent   Forum des professionnels en informatique > Webmasters - Développement Web > JavaScript
JavaScript Forum programmation JavaScript. Lire : Cours JavaScript, FAQ JavaScript, Toutes les FAQ JavaScript et Sources JavaScript
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 18/01/2012, 01h02   #1
Invité de passage
 
Francois Dulude
Inscription : janvier 2010
Messages : 7
Détails du profil
Informations personnelles :
Nom : Francois Dulude

Informations forums :
Inscription : janvier 2010
Messages : 7
Points : 1
Points : 1
Par défaut Fermer un DIV quand on clique en dehors

Bonjour,
J'ai trouvé sur le net une tonne d'exemple qui me permettent de fermer un DIV quand le clique a l'extérieur de celui-ci !
Je poste ici parce que que je n'ai rien trouvé qui règle mon problème !

Voici la page qui contient mon menu : http://www.d3cup.com/toolbar.php

Mon lien est fait sur une image qui ouvre un DIV. quand on reclique sur limage, le div se referme.
A chaque fois que j'ajoute un code qui permet de fermer le DIV quand on clique a l'extérieur, le reclique sur limage pour fermer le DIV ne fontionne plus ... !!

J'ai probablement un problème a quelque part .. Je ne suis pas très bon en Javascript..

Voici mon code html pour ouvrire le DIV
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
    <div>
      <ul>
        <li id="ico" class="icon"><a href="index.php?profile=<?= $id; ?>"><img border="0" src="ico/profile.png" title="<? echo $pseudo; ?>'s profile"></a></li>
        <li class="separator">&nbsp;</li>
        <li id="ico" class="icon"><a id="ico2" href="javascript:callplayed('played')"><img border="0" src="ico/tounoijoue.png" title="Tournaments played"></a></li>
        <li id="ico" class="icon"><a id="ico3" href="javascript:callnextgame('nextgame')"><img border="0" src="ico/nextgame.png" title="Next Game"></a></li>
        <li id="ico" class="icon"><a id="ico4" href="javascript:callversuschat('versuschat')"><img border="0" src="ico/versuschat.png" title="Versus Chat"></a></li>
        <li id="ico" class="icon"><a id="ico5" href="javascript:callteam('team')"><img border="0" src="ico/team.png" title="Team"></a></li>
        <li id="ico" class="icon"><a id="ico6" href="javascript:callfriends('friends')"><img border="0" src="ico/friends.png" title="Friends"></a></li> 
 
        <li class="chat"><? 
		$h1 = date("G");
		$h = $h1 + 3;
		$m = date("i");
		$time = "$h:$m";
		echo $time; ?></li>
        <li id="ico" class="icon-chat"><a href="disconnect.php"><img border="0" src="ico/logout.png" title="Logout"></a></li>
        <li class="chat separator">&nbsp;</li>
      </ul>
    </div>
Voici mon code javascript fait comme je peux..
Code :
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
 
function callplayed(id) {
	if (document.getElementById){
		obj = document.getElementById(id);
			if (obj.style.display == "none") {
	            obj.style.display = "";
				document.getElementById("ico2").style.background = "#C0C0C0";
				<?php if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ) { ?>
				document.getElementById("ico2").style.paddingTop = "17px";
				<?php } else { ?>
				document.getElementById("ico2").style.paddingTop = "16px";
				<?php } ?>
				document.getElementById("ico2").style.paddingLeft = "2px";
				document.getElementById("ico2").style.paddingRight = "2px";
				document.getElementById("ico2").style.borderLeft = "1px solid #333333";
				document.getElementById("ico2").style.borderRight = "1px solid #333333";
				document.getElementById("ico2").style.borderBottom = "1px solid #333333";
 
				document.getElementById("nextgame").style.display = "none";
				document.getElementById("versuschat").style.display = "none";
				document.getElementById("team").style.display = "none";
				document.getElementById("friends").style.display = "none";
				document.getElementById("ico3").style.background = "";
				document.getElementById("ico3").style.borderLeft = "1px solid transparent";
				document.getElementById("ico3").style.borderRight = "1px solid transparent";
				document.getElementById("ico3").style.borderBottom = "1px solid transparent";
				document.getElementById("ico4").style.background = "";
				document.getElementById("ico4").style.borderLeft = "1px solid transparent";
				document.getElementById("ico4").style.borderRight = "1px solid transparent";
				document.getElementById("ico4").style.borderBottom = "1px solid transparent";
				document.getElementById("ico5").style.background = "";
				document.getElementById("ico5").style.borderLeft = "1px solid transparent";
				document.getElementById("ico5").style.borderRight = "1px solid transparent";
				document.getElementById("ico5").style.borderBottom = "1px solid transparent";
				document.getElementById("ico6").style.background = "";
				document.getElementById("ico6").style.borderLeft = "1px solid transparent";
				document.getElementById("ico6").style.borderRight = "1px solid transparent";
				document.getElementById("ico6").style.borderBottom = "1px solid transparent";
 
			} else {
	        if (obj.style.display == ""){
	            obj.style.display = "none";
				document.getElementById("ico2").style.background = "";
				document.getElementById("ico2").style.borderLeft = "1px solid transparent";
				document.getElementById("ico2").style.borderRight = "1px solid transparent";
				document.getElementById("ico2").style.borderBottom = "1px solid transparent";
	        }
			}
	}
}
function callnextgame(id) {
	if (document.getElementById){
		obj = document.getElementById(id);
			if (obj.style.display == "none") {
	            obj.style.display = "";
				document.getElementById("ico3").style.background = "#C0C0C0";
				<?php if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ) { ?>
				document.getElementById("ico3").style.paddingTop = "17px";
				<?php } else { ?>
				document.getElementById("ico3").style.paddingTop = "16px";
				<?php } ?>
				document.getElementById("ico3").style.paddingLeft = "2px";
				document.getElementById("ico3").style.paddingRight = "2px";
				document.getElementById("ico3").style.borderLeft = "1px solid #333333";
				document.getElementById("ico3").style.borderRight = "1px solid #333333";
				document.getElementById("ico3").style.borderBottom = "1px solid #333333";
 
				document.getElementById("played").style.display = "none";
				document.getElementById("versuschat").style.display = "none";
				document.getElementById("team").style.display = "none";
				document.getElementById("friends").style.display = "none";
				document.getElementById("ico2").style.background = "";
				document.getElementById("ico2").style.borderLeft = "1px solid transparent";
				document.getElementById("ico2").style.borderRight = "1px solid transparent";
				document.getElementById("ico2").style.borderBottom = "1px solid transparent";
				document.getElementById("ico4").style.background = "";
				document.getElementById("ico4").style.borderLeft = "1px solid transparent";
				document.getElementById("ico4").style.borderRight = "1px solid transparent";
				document.getElementById("ico4").style.borderBottom = "1px solid transparent";
				document.getElementById("ico5").style.background = "";
				document.getElementById("ico5").style.borderLeft = "1px solid transparent";
				document.getElementById("ico5").style.borderRight = "1px solid transparent";
				document.getElementById("ico5").style.borderBottom = "1px solid transparent";
				document.getElementById("ico6").style.background = "";
				document.getElementById("ico6").style.borderLeft = "1px solid transparent";
				document.getElementById("ico6").style.borderRight = "1px solid transparent";
				document.getElementById("ico6").style.borderBottom = "1px solid transparent";
 
 
			} else {
	        if (obj.style.display == ""){
	            obj.style.display = "none";
				document.getElementById("ico3").style.background = "";
				document.getElementById("ico3").style.borderLeft = "1px solid transparent";
				document.getElementById("ico3").style.borderRight = "1px solid transparent";
				document.getElementById("ico3").style.borderBottom = "1px solid transparent";
	        }
			}
	}
}
function callversuschat(id) {
	if (document.getElementById){
		obj = document.getElementById(id);
			if (obj.style.display == "none") {
	            obj.style.display = "";
				document.getElementById("ico4").style.background = "#C0C0C0";
				<?php if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ) { ?>
				document.getElementById("ico4").style.paddingTop = "17px";
				<?php } else { ?>
				document.getElementById("ico4").style.paddingTop = "16px";
				<?php } ?>
				document.getElementById("ico4").style.paddingLeft = "2px";
				document.getElementById("ico4").style.paddingRight = "2px";
				document.getElementById("ico4").style.borderLeft = "1px solid #333333";
				document.getElementById("ico4").style.borderRight = "1px solid #333333";
				document.getElementById("ico4").style.borderBottom = "1px solid #333333";
 
				document.getElementById("nextgame").style.display = "none";
				document.getElementById("played").style.display = "none";
				document.getElementById("team").style.display = "none";
				document.getElementById("friends").style.display = "none";
				document.getElementById("ico3").style.background = "";
				document.getElementById("ico3").style.borderLeft = "1px solid transparent";
				document.getElementById("ico3").style.borderRight = "1px solid transparent";
				document.getElementById("ico3").style.borderBottom = "1px solid transparent";
				document.getElementById("ico2").style.background = "";
				document.getElementById("ico2").style.borderLeft = "1px solid transparent";
				document.getElementById("ico2").style.borderRight = "1px solid transparent";
				document.getElementById("ico2").style.borderBottom = "1px solid transparent";
				document.getElementById("ico5").style.background = "";
				document.getElementById("ico5").style.borderLeft = "1px solid transparent";
				document.getElementById("ico5").style.borderRight = "1px solid transparent";
				document.getElementById("ico5").style.borderBottom = "1px solid transparent";
				document.getElementById("ico6").style.background = "";
				document.getElementById("ico6").style.borderLeft = "1px solid transparent";
				document.getElementById("ico6").style.borderRight = "1px solid transparent";
				document.getElementById("ico6").style.borderBottom = "1px solid transparent";
 
 
			} else {
	        if (obj.style.display == ""){
	            obj.style.display = "none";
				document.getElementById("ico4").style.background = "";
				document.getElementById("ico4").style.borderLeft = "1px solid transparent";
				document.getElementById("ico4").style.borderRight = "1px solid transparent";
				document.getElementById("ico4").style.borderBottom = "1px solid transparent";
	        }
			}
	}
}
function callteam(id) {
	if (document.getElementById){
		obj = document.getElementById(id);
			if (obj.style.display == "none") {
	            obj.style.display = "";
				document.getElementById("ico5").style.background = "#C0C0C0";
				<?php if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ) { ?>
				document.getElementById("ico5").style.paddingTop = "17px";
				<?php } else { ?>
				document.getElementById("ico5").style.paddingTop = "16px";
				<?php } ?>
				document.getElementById("ico5").style.paddingLeft = "2px";
				document.getElementById("ico5").style.paddingRight = "2px";
				document.getElementById("ico5").style.borderLeft = "1px solid #333333";
				document.getElementById("ico5").style.borderRight = "1px solid #333333";
				document.getElementById("ico5").style.borderBottom = "1px solid #333333";
 
				document.getElementById("nextgame").style.display = "none";
				document.getElementById("versuschat").style.display = "none";
				document.getElementById("played").style.display = "none";
				document.getElementById("friends").style.display = "none";
				document.getElementById("ico3").style.background = "";
				document.getElementById("ico3").style.borderLeft = "1px solid transparent";
				document.getElementById("ico3").style.borderRight = "1px solid transparent";
				document.getElementById("ico3").style.borderBottom = "1px solid transparent";
				document.getElementById("ico4").style.background = "";
				document.getElementById("ico4").style.borderLeft = "1px solid transparent";
				document.getElementById("ico4").style.borderRight = "1px solid transparent";
				document.getElementById("ico4").style.borderBottom = "1px solid transparent";
				document.getElementById("ico2").style.background = "";
				document.getElementById("ico2").style.borderLeft = "1px solid transparent";
				document.getElementById("ico2").style.borderRight = "1px solid transparent";
				document.getElementById("ico2").style.borderBottom = "1px solid transparent";
				document.getElementById("ico6").style.background = "";
				document.getElementById("ico6").style.borderLeft = "1px solid transparent";
				document.getElementById("ico6").style.borderRight = "1px solid transparent";
				document.getElementById("ico6").style.borderBottom = "1px solid transparent";
 
 
			} else {
	        if (obj.style.display == ""){
	            obj.style.display = "none";
				document.getElementById("ico5").style.background = "";
				document.getElementById("ico5").style.borderLeft = "1px solid transparent";
				document.getElementById("ico5").style.borderRight = "1px solid transparent";
				document.getElementById("ico5").style.borderBottom = "1px solid transparent";
	        }
			}
	}
}
function callfriends(id) {
	if (document.getElementById){
		obj = document.getElementById(id);
			if (obj.style.display == "none") {
	            obj.style.display = "";
				document.getElementById("ico6").style.background = "#C0C0C0";
				<?php if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ) { ?>
				document.getElementById("ico6").style.paddingTop = "17px";
				<?php } else { ?>
				document.getElementById("ico6").style.paddingTop = "16px";
				<?php } ?>
				document.getElementById("ico6").style.paddingLeft = "2px";
				document.getElementById("ico6").style.paddingRight = "2px";
				document.getElementById("ico6").style.borderLeft = "1px solid #333333";
				document.getElementById("ico6").style.borderRight = "1px solid #333333";
				document.getElementById("ico6").style.borderBottom = "1px solid #333333";
 
				document.getElementById("nextgame").style.display = "none";
				document.getElementById("versuschat").style.display = "none";
				document.getElementById("team").style.display = "none";
				document.getElementById("played").style.display = "none";
				document.getElementById("ico3").style.background = "";
				document.getElementById("ico3").style.borderLeft = "1px solid transparent";
				document.getElementById("ico3").style.borderRight = "1px solid transparent";
				document.getElementById("ico3").style.borderBottom = "1px solid transparent";
				document.getElementById("ico4").style.background = "";
				document.getElementById("ico4").style.borderLeft = "1px solid transparent";
				document.getElementById("ico4").style.borderRight = "1px solid transparent";
				document.getElementById("ico4").style.borderBottom = "1px solid transparent";
				document.getElementById("ico5").style.background = "";
				document.getElementById("ico5").style.borderLeft = "1px solid transparent";
				document.getElementById("ico5").style.borderRight = "1px solid transparent";
				document.getElementById("ico5").style.borderBottom = "1px solid transparent";
				document.getElementById("ico2").style.background = "";
				document.getElementById("ico2").style.borderLeft = "1px solid transparent";
				document.getElementById("ico2").style.borderRight = "1px solid transparent";
				document.getElementById("ico2").style.borderBottom = "1px solid transparent";
 
 
			} else {
	        if (obj.style.display == ""){
	            obj.style.display = "none";
				document.getElementById("ico6").style.background = "";
				document.getElementById("ico6").style.borderLeft = "1px solid transparent";
				document.getElementById("ico6").style.borderRight = "1px solid transparent";
				document.getElementById("ico6").style.borderBottom = "1px solid transparent";
	        }
			}
	}
}
Merci d'avance !
poinball est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 18/01/2012, 10h47   #2
Rédacteur/Modérateur
 
Avatar de SpaceFrog
 
Homme
Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Analyste Programmeur
Inscription : mars 2002
Messages : 30 071
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : Royaume-Uni

Informations professionnelles :
Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Analyste Programmeur
Secteur : Industrie

Informations forums :
Inscription : mars 2002
Messages : 30 071
Points : 45 201
Points : 45 201
l'équivalent d'un onblur() sur un div quoi ...

Une petite recherche t'aurait mené par là
http://www.developpez.net/forums/d11...nt-onblur-div/
__________________
Ma page Developpez
Président du CCMPTP (Comité Contre le Mot "Problème" dans les Titres de Posts)
Deux règles du succès: 1) Ne communiquez jamais à quelqu'un tout votre savoir...
Votre post est résolu ? Alors n'oubliez pas le Tag


réalisations :www.planet-languages.com|www.saftair.com| www.ouestisol.fr | www.sebemex.fr | www.extramiante.fr | www.sistac-alizay.fr | www.acoustishop.fr | www.litt.fr | www.ouestventil.fr
SpaceFrog est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 18/01/2012, 17h59   #3
Invité de passage
 
Francois Dulude
Inscription : janvier 2010
Messages : 7
Détails du profil
Informations personnelles :
Nom : Francois Dulude

Informations forums :
Inscription : janvier 2010
Messages : 7
Points : 1
Points : 1
J'ai déja testé ce code.. il ne fonctionne pas.
Tu peux aller vérifier sur le site je l'ais appliqué sur l'icone "Tournaments played"

Les 2 erreurs habituelle :
1. Quand on reclique sur l'icone pour refermer le DIV sa ne marche plus
2. Quand on clique sur le DIV sa le ferme

Voici le javascript ajouté a l'autre plus haut
Code :
1
2
3
4
5
6
7
8
9
10
 
<script type="text/javascript">
var oDiv = document.getElementById('played');
document.body.onclick = function(e){
  var oElem = e ? e.target : event.srcElement;
  if( oElem !== oDiv){
    oDiv.style.display = 'none'
  }
}
</script>
Merci quand meme ..
poinball est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 18/01/2012, 18h31   #4
Invité de passage
 
Francois Dulude
Inscription : janvier 2010
Messages : 7
Détails du profil
Informations personnelles :
Nom : Francois Dulude

Informations forums :
Inscription : janvier 2010
Messages : 7
Points : 1
Points : 1
Ce que je cherche a faire est simple.

Comme sur facebook avec son menu du top ou meme google !

un Icones en PNG.
Quand on clique sur un l'icone le Div apparais.
Pour le faire disparaitre on a 2 choix. Soit cliquer a lextérieur du DIV ou recliquer sur l'icone.

Je n'ais rien trouvé qui fonctionne a 100% :S auriez-vous un exemple ?
poinball est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 18/01/2012, 19h35   #5
Invité de passage
 
Francois Dulude
Inscription : janvier 2010
Messages : 7
Détails du profil
Informations personnelles :
Nom : Francois Dulude

Informations forums :
Inscription : janvier 2010
Messages : 7
Points : 1
Points : 1
J'ai trouvé un script qui marche en jQuery.
Le seul problème qui me reste à régler c'est mon bouton qui ne fait pas partie de mon DIV, alors quand je reclique dessus, le div se referme et s'ouvre de nouveau.

Se referme parce que je clique en dehors du DIV.
Se réouvre parce que je clique sur le bouton d'ouverture du DIV.

Voici le code jQuery :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
<script>
function ClickOutsideCheck(e)
{
  var el = e.target;
  var popup = $('.form_wrapper:visible')[0];
  if(popup==undefined) return true;
 
  while (true){
    if (el == popup ) {
      return true;
    } else if (el == document) {
      $(".form_wrapper").hide();
      return false;
    } else {
      el = $(el).parent()[0];
    }
  }
};
 
$(document).bind('mousedown.popup', ClickOutsideCheck);
</script>
Je ne comprends pas très bien le code alors...
Comment pourrais-je arranger sa ?
poinball est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/01/2012, 09h51   #6
Membre éprouvé
 
Avatar de yjuliet
 
Homme Yvan
Consultant informatique
Inscription : août 2006
Messages : 360
Détails du profil
Informations personnelles :
Nom : Homme Yvan
Âge : 32
Localisation : France

Informations professionnelles :
Activité : Consultant informatique
Secteur : High Tech - Produits et services télécom et Internet

Informations forums :
Inscription : août 2006
Messages : 360
Points : 402
Points : 402
Il faut que tu ajoutes dans la méthode d'ouverture de la div une vérification de l'état de ton div, soit avec une variable d'état que tu ajoutes, soit en fonction de l'état que tu saurais détecter...
__________________
yjuliet est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/01/2012, 15h49   #7
Invité de passage
 
Francois Dulude
Inscription : janvier 2010
Messages : 7
Détails du profil
Informations personnelles :
Nom : Francois Dulude

Informations forums :
Inscription : janvier 2010
Messages : 7
Points : 1
Points : 1
L'ouverture du DIV vérifie déjà que le DIV ne soit pas ouvert avant. Mais je ne vois pas quoi mettre pour vérifier s'il vient tout juste d'être fermé par le clickout. :S
poinball est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/01/2012, 16h25   #8
Rédacteur/Modérateur
 
Avatar de SpaceFrog
 
Homme
Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Analyste Programmeur
Inscription : mars 2002
Messages : 30 071
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : Royaume-Uni

Informations professionnelles :
Activité : Développeur Web Php Mysql Html Javascript CSS Apache - Intégrateur - Analyste Programmeur
Secteur : Industrie

Informations forums :
Inscription : mars 2002
Messages : 30 071
Points : 45 201
Points : 45 201
tu utilises jquery ?
=>
__________________
Ma page Developpez
Président du CCMPTP (Comité Contre le Mot "Problème" dans les Titres de Posts)
Deux règles du succès: 1) Ne communiquez jamais à quelqu'un tout votre savoir...
Votre post est résolu ? Alors n'oubliez pas le Tag


réalisations :www.planet-languages.com|www.saftair.com| www.ouestisol.fr | www.sebemex.fr | www.extramiante.fr | www.sistac-alizay.fr | www.acoustishop.fr | www.litt.fr | www.ouestventil.fr
SpaceFrog est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 06h35.


 
 
 
 
Partenaires

Hébergement Web