Précédent   Forum des professionnels en informatique > Webmasters - Développement Web > JavaScript > Bibliothèques & Frameworks > jQuery
jQuery Forum d'entraide sur le framework jQuery. Avant de poster : Tutoriels jQuery, FAQ jQuery, Tous les tutoriels JavaScript, Toutes les FAQ 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 24/06/2011, 15h29   #1
Candidat au titre de Membre du Club
 
Développeur Web
Inscription : janvier 2011
Messages : 48
Détails du profil
Informations professionnelles :
Activité : Développeur Web

Informations forums :
Inscription : janvier 2011
Messages : 48
Points : 13
Points : 13
Par défaut append et onclick

Bonjour,
je voudrais inserer une image muni d'un onclick.

du genre:

Code :
$("#div1").append("<img src='monimage.png' onclick='fonction(attribut)' ");
Pourriez vous m'aider a avoir la bonne syntaxe, car cela bloque et je pense que cela vient juste d'un probleme de " et '.
merci d'avance
Kirua76 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/06/2011, 19h28   #2
Membre éclairé
 
Homme
Webmaster
Inscription : septembre 2007
Messages : 376
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France

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

Informations forums :
Inscription : septembre 2007
Messages : 376
Points : 377
Points : 377
Salut,

il manque la fermeture de ta balise img

Code :
1
2
$("#div1").append("<img src='monimage.png' onclick='fonction(attribut)' /> ");
Finality est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/06/2011, 09h14   #3
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 019
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 019
Points : 45 114
Points : 45 114
ou tant qu'a faire quelquechose de div1 (jeu de mots )
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
<script type="text/javascript">
function fonction(t){alert(t)}
$(function(){
$("#div1").append( $('<img />').attr("src","monimage.png").click( function(){fonction('attribut')} ) )
 
})</script>
</head>
 
<body>
<div id="div1"></div>
</body>
 
</html>
__________________
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 27/06/2011, 09h42   #4
Candidat au titre de Membre du Club
 
Développeur Web
Inscription : janvier 2011
Messages : 48
Détails du profil
Informations professionnelles :
Activité : Développeur Web

Informations forums :
Inscription : janvier 2011
Messages : 48
Points : 13
Points : 13
aucune des deux réponses ne convient désolé.

Je dois insérer une ligne dans un tableau de la forme suivante:
Code :
 $("#table_cc").append("<tr id='cc" + nb_cc + "'><td id='td_cc" + nb_cc + "'>" + mailcc + "</td><td><img src='images/icon-delete.png' onclick='fonction()'/></td></tr>");
avec nb_cc et mailcc des variables.

Pourriez vous si vous savez me donner la syntaxe exacte svp
Kirua76 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/06/2011, 09h58   #5
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 019
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 019
Points : 45 114
Points : 45 114
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<script type="text/javascript">
function fonction(obj){alert(obj.parentNode.parentNode.id)}
nb_cc_tab=new Array('nb_cc_a','nb_cc_b','nb_cc_c','nb_cc_d')
mailcc_tab=new Array("mailcc_a","mailcc_b","mailcc_c","mailcc_d")
$(function(){
var i=-1
while (nb_cc_tab[++i]){
mailcc=mailcc_tab[i]
nb_cc=nb_cc_tab[i]
$("#table_cc").append('<tr id="cc' + nb_cc + '"><td id="td_cc' + nb_cc + '">' + mailcc + '</td><td><img src="images/icon-delete.png" onclick="fonction(this)"/></td></tr>');
}
})
</script>
</head>
 
<body>
<table id="table_cc"></table>
</body>
__________________
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 01
Vieux 27/06/2011, 12h58   #6
Candidat au titre de Membre du Club
 
Développeur Web
Inscription : janvier 2011
Messages : 48
Détails du profil
Informations professionnelles :
Activité : Développeur Web

Informations forums :
Inscription : janvier 2011
Messages : 48
Points : 13
Points : 13
j'ai essayé la syntaxe, mais lorsque je clique sur le bouton, j'ai toujours la même erreur que visual studio me renvoit: objet attendu en faisant un focus sur la ligne DocType...

Code :
$("#table_cc").append('<tr id="cc' + nb_cc + '"><td id="td_cc' + nb_cc + '">' + mailcc + '</td><td><img src="images/icon-delete.png" onclick="deletecc('+nb_cc+')"/></td></tr>');
la ligne est elle bonne?
Kirua76 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/06/2011, 23h48   #7
Rédacteur
 
Avatar de danielhagnoul
 
Homme Daniel Hagnoul
Étudiant perpétuel
Inscription : février 2009
Messages : 3 221
Détails du profil
Informations personnelles :
Nom : Homme Daniel Hagnoul
Âge : 61
Localisation : Belgique

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

Informations forums :
Inscription : février 2009
Messages : 3 221
Points : 6 767
Points : 6 767
Bonsoir

Il y a plusieurs remarques à écrire sur vos deux lignes de code, mais sur le forum jQuery, on fait du jQuery !

Exemple fonctionnel, il suffit de copier-coller le code pour le tester.

Si vous ne comprenez pas le code, je vous engage à lire la FAQ jQuery (lien dans la signature et en haut de la page).

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
<!doctype html>
<html lang="fr">
<head>
	<meta charset="utf-8">
	<meta name="Author" content="Daniel Hagnoul">
	<title>Forum jQuery</title>
	<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/themes/humanity/jquery-ui.css">
	<link rel="stylesheet" href="http://danielhagnoul.developpez.com/lib/jPicker/css/jPicker.dvjh-1.1.6.min.css" />
	<style>
		/* Base */
		div,p,h1,h2,h3,h4,h5,h6,ul,ol,dl,form,table,img {margin:0px; padding:0px; }
		body {background-color:rgba(210, 214, 98, 0.5); color:#000000; font-family:sans-serif; font-size:medium; font-style:normal; font-weight:normal; line-height:normal; letter-spacing:normal; }
		h1,h2,h3,h4,h5 {font-family:serif; padding:6px; }
		p, div, td {word-wrap:break-word; }
		pre, code {white-space:pre-wrap; word-wrap:break-word; }
		img, input, textarea, select {max-width:100%; }
        img {border:none; }
		h1 {font-size:2em; text-shadow: 4px 4px 4px #bbbbbb; text-align:center; }
		p {padding:6px; }
        ul,ol,dl {list-style:none; padding-left:6px; padding-top:6px; }
        li {padding-bottom:6px; }
		.conteneur {width:95%; min-width:800px; min-height:400px; margin:12px auto; background-color:#FFFFFF; color:#000000; border:1px solid #666666; }
 
		/* article */
		/*
		.conteneur {border-spacing:4px; }
		.ligne {display:table-row; }
		article {display:table-cell; text-align:center; vertical-align:middle; border:1px dotted grey; }
		article:nth-of-type(1) {width:400px; height:120px; }
		*/
 
		/* -- */
		#table_cc td {border:1px dotted grey; padding:6px;}
	</style>
</head>
<body>
	<h1>Forum jQuery</h1>
	<section class="conteneur">
 
		<table id="table_cc">
			<tbody>
 
			</tbody>
		</table>
 
	</section>
	<footer itemscope itemtype="http://data-vocabulary.org/Person">
		<time datetime="2011-06-28T23:40:00.000+02:00" pubdate>2011-06-28</time> <span itemprop="name">Daniel Hagnoul</span> <a href="http://www.developpez.net/forums/u285162/danielhagnoul/" itemprop="url">@danielhagnoul</a>
	</footer>
	<script charset="utf-8" src="http://code.jquery.com/jquery-1.6.2rc1.min.js"></script>
	<script charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/i18n/jquery-ui-i18n.min.js"></script>
	<script charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>
	<script charset="utf-8" src="http://danielhagnoul.developpez.com/lib/jPicker/jpicker-1.1.6.min.js"></script>
	<script>
		$(function(){
			/* -- */
 
			/*
			 * Sur le forum jQuery, la réponse sera du jQuery !
			 * Exemple :
			 */
			var nb_cc = 24057, // attention, un ID doit être unique dans la page web
				mailcc = "moi@ici.ou";
 
			$("<tr/>", {
				"id": "cc" + nb_cc,
				"html": '<td id="td_cc' + nb_cc + '">' + mailcc + '</td><td></td>'
			}).appendTo("#table_cc > tbody");
 
			$("<img/>", {
				"src": "http://danielhagnoul.developpez.com/images/imageTest.png",
				"click": function(){
					alert("Bonsoir !\nL'image a été cliquée.");
				}
			}).appendTo($("#td_cc"+nb_cc).next("td"));
 
			/*
			 * Ce qui donne :
			 * 
			 * <table id="table_cc">
			 * 		<tbody>
			 * 			<tr id="cc24057">
			 * 				<td id="td_cc24057">moi@ici.ou</td>
			 * 				<td><img src="http://danielhagnoul.developpez.com/images/imageTest.png"></td>
			 * 			</tr>
			 * 		</tbody>
			 * 	</table>
			 */
 
 
			/* jPicker : http://www.digitalmagicpro.com/jPicker/, outil pour choisir rapidement une couleur */
			$.fn.jPicker.defaults.images.clientPath="http://danielhagnoul.developpez.com/lib/jPicker/images/";
			$(".conteneur").jPicker({window:{expandable:true,title:"jPicker : choissisez une couleur :",
			alphaSupport:true,position:{x:'screenCenter',y:'top'}},color:{ active:new $.jPicker.Color({ r: 210, g: 214, b: 98, a: 128 })}},
			function(color, context){var c = color.val("all");if (c){$("body").css("backgroundColor", "rgba(" +
			c.r + "," + c.g + "," + c.b + "," + (c.a/255).toFixed(2) + ")");}});
		});
	</script>
</body>  
</html>
__________________

FAQ jQuery

Mon cahier d’exercices sur jQuery & Co

Si un message vous a aidé ou vous semble pertinent, votez pour lui !
danielhagnoul est déconnecté   Envoyer un message privé Réponse avec citation 01
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 01h52.


 
 
 
 
Partenaires

Hébergement Web