创建一个跨度并向其注入图像

时间:2009-10-13 19:33:43

标签: image mootools html

我是mootools的新手。我想要的是创建一个新的元素跨度并为其注入图像。

我编写了以下代码但不起作用:

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');

即使是“我是新div”的文字也没有显示。

由于

1 个答案:

答案 0 :(得分:1)

您的问题是您尝试使用其ID将图像注入跨区,但尚未将跨度添加到页面(DOM树)。试试这个:

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