2009-10-13 7 views
0

Je suis nouveau sur mootools. Ce que je veux, c'est créer un nouvel élément et lui injecter une image.créer un span et injecter une image

j'ai écrit le code suivant, mais ne fonctionne pas:

var newElementVar = new Element('span', { 
    'id': 'id_namekhan', 
    'text': 'I am a new div' 
}); 
var my_img = new Element ('img' , { 
    'src' :'uploading/'+json.get('fpath')+'' , 
    'style' : 'width:50px; text-align:left' 
}).inject(id_namekhan, 'top'); 

Même le texte « Je suis un nouveau div » est pas à l'affiche.

Merci

+0

Vous devrez fournir le code que vous utilisez pour json.get ('fpath') – keif

Répondre

1

Votre problème est que vous essayez d'injecter l'image dans l'espace en utilisant son ID, mais la durée n'a pas été ajouté à la page (arborescence DOM) encore. Essayez ceci:

var my_span = new Element('span', {'id': 'id_namekhan','text': 'I am a new div'}); 
var my_img = new Element ('img' , {'src' :'uploading/'+json.get('fpath')+'' , 'style' : 'width:50px; text-align:left' }).inject(my_span, 'top'); 
my_span.inject($('element')); // replace element with the ID of the element you wish to inject the span inside 
Questions connexes