如何使用jQuery将变量中的图像附加到列表中

时间:2012-10-03 09:34:11

标签: jquery jquery-mobile

我试过这种方式,但它不起作用:

  var imglunch = new Image();
  imglunch.src = "../images/faciliteit_lunch.png";
  var imgdiner = new Image();
  imgdiner.src = "../images/faciliteit_diner.png";
  $('.greenfluxlist').append('<li id="' + element.Identifier + '"><a href="bla.html" rel="external">' + imglunch + imgdiner + '</a></li>');

任何人都知道如何添加它?

1 个答案:

答案 0 :(得分:3)

只需让我们使用jQuery函数来创建你的img元素:

var imglunch = $( '<img />' );
imglunch.attr( 'src', '../images/faciliteit_lunch.png' );

var imgdiner = $( '<img />' );
imgdiner.attr( 'src', '../images/faciliteit_diner.png' );

$('.greenfluxlist').append('<li id="' + element.Identifier + '"><a href="bla.html" rel="external"></a></li>');
$( '#' + element.Identifier + ' > a' ).append( imglunch ).append( imgdiner ); 

或者你可以一次性附加你的html代码:

$('.greenfluxlist').append('<li id="' + element.Identifier + '"><a href="bla.html" rel="external"><img src="../images/faciliteit_lunch.png" /><img src="../images/faciliteit_diner.png" /></a></li>');