jQuery附加img和一个

问题描述

| 我需要将img附加到
<a>
标记内。我一直对此无能为力,这让我发疯。如果您能提供帮助,那将很好。我正在使用jQuery 1.5
while condition{
    $(\"<img>\").attr(\"src\",thumb).appendTo(\"#images\");
}
最终结果应为:
<a href=\"#\"><img src=\"xxxxx\"></a>
当我使用prepend或appendto时,我得到以下结果:
<div id=\"images src=\"xxxxxxx\"><a></a> 
OR
<a href=\"#\"></a><img src=\"xxxxx\">
感谢您的协助。     

解决方法

$(\"#images\").append($(\"<a>\",{
    href: \"#\",html: $(\"<img>\",{ src: thumb })
}));
工作示例:http://jsfiddle.net/hunter/Rxkxg/     ,
$(\'<a>\') // create anchor first
  .attr(\'href\',\'#\') // set anchor HREF attribute
  .append( // inside it,append an image
    $(\'<img>\') // new image
      .attr(\'src\',thumb) // set image SRC attribute
  ) // end append
  .appendTo(\'#images\') // add to image body
几乎在正确的轨道上... 演示