2011-04-28 67 views
2

我做了以下内容:jQuery的追加问题

$("#fb_friends").append("<div style=\"width:150px;float:left;font-size:11px;color:White;\">"); 
$("#fb_friends").append("<input type=\"checkbox\" name=\"friends\" value=\"1244524\" />"); 
$("#fb_friends").append("<img src=\"http://graph.facebook.com/" + friend.id + "/picture\" alt=\"Picture\" style=\"width:24px\">"); 
$("#fb_friends").append(friend.name); 
$("#fb_friends").append("</div>"); 
然而

在我的HTML,我所得到的是:

<div style="width:150px;float:left;font-size:11px;color:White;"></div> 
<input type="checkbox" name="friends" value="1244524"> 
<img src="http://graph.facebook.com/1244524/picture" alt="Picture" style="width:24px"> 
"friend name" 

为什么会出现在第一线关闭div标签?

UPDATE:

我试着这样做,而不是:

$("#fb_friends").append("<div class=\"friend\" style=\"width:150px;float:left;font-size:11px;color:White;\">"); 
$(".friend").append("<input type=\"checkbox\" name=\"friends\" value=\"1244524\" />"); 
$(".friend").append("<img src=\"http://graph.facebook.com/" + friend.id + "/picture\" alt=\"Picture\" style=\"width:24px\">"); 
$(".friend").append(friend.name); 

男孩,它是缓慢的地狱

回答

3

追加添加一个元素,而不是文字。

您想要创建div,并将其他元素附加到div。

var div = $('<div />').css({width:'150px' /*, ... */}); 
$('<input />', {"type":"checkbox", "name":"friends", value:"1244524" }).appendTo(div); 
$('<img />', {"src":"http://graph.facebook.com/" + friend.id + "/picture", alt: "Picture" }) 
    .style(width: '24px') 
    .appendTo(div); 
div.append(friend.name); 

$("#fb_friends").append(div); 

如果评价者创建HTML,这也是可能的:

var html = "<div style=\"width:150px;float:left;font-size:11px;color:White;\">" + 
      "<input type=\"checkbox\" name=\"friends\" value=\"1244524\" />" + 
      "<img src=\"http://graph.facebook.com/" + friend.id + "/picture\" alt=\"Picture\" style=\"width:24px\">" + 
      friend.name + "</div>"; 
$("#fb_friends").html(html); 
+0

我怎么做,在这个案例? – adit 2011-04-28 18:05:12

+0

@Brad - 我编辑了很多,它之前没有,所以这个问题是合法的。但是,谢谢! – Kobi 2011-04-28 18:12:25

+1

我得到一个未捕获的SyntaxError:意外的标记= at,alt =“图片” – adit 2011-04-28 18:17:20

0

@Kobi写了一个很好的解决方案,另一种解决方案是将所有内容写在一个追加