开发者

jQuery append issue

开发者 https://www.devze.com 2023-03-01 14:47 出处:网络
I am doing the following: $(\"#fb_friends\").append(\"<div style=\\\"width:150px;float:left;font-size:11px;color:White;\\\">\");

I am doing the following:

$("#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(frien开发者_开发技巧d.name);
$("#fb_friends").append("</div>");

however in my html, what I get is:

<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"

Why is there a closing div tag at the first line?

UPDATE:

I tried doing this instead:

$("#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);

boy, it's slow as hell


Append adds an element, not text.

You want to create the div, and append other element to the 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);

If you rater create the html, that's also possible:

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


@Kobi wrote a nice solution, another solution is to write everything in one append

0

精彩评论

暂无评论...
验证码 换一张
取 消