访问动态添加的元素

时间:2015-03-26 15:58:24

标签: jquery dom

var $singleMember = '<a class="singleMember"><img class="avatar"><p class="username"></p></a>';

$singleMember.attr('href',member.link);
$singleMember.children('.avatar').attr('src',member.avatar);
$singleMember.children('.username').html(member.name);

为什么抛出&#34; undefined不是函数&#34;?

1 个答案:

答案 0 :(得分:1)

var $singleMember只是一个字符串,因此.attr().children()无效(因此您看到错误)

将其切换为:

   var singleMember = '<a class="singleMember"><img class="avatar"><p    class="username"></p></a>';
   var $singleMember = $(singleMember);

   $singleMember.attr('href',member.link);
   $singleMember.children('.avatar').attr('src',member.avatar);
   $singleMember.children('.username').html(member.name);