admin管理员组文章数量:1290956
$('#test-list').append($(document.createElement("li")).attr({id: data.msg}));
$('#'+data.msg).append($(document.createElement("img")).attr({src: "kep.php?kep=upload/"+data.msg+"&w=180;&h=150;"}));
Does not work, because of the $('#'+data.msg).
param. I don't know how to fix it. I want to make a sub-element to the #test-list and name it to the value of data.msg variable.
$('#test-list').append($(document.createElement("li")).attr({id: data.msg}));
$('#'+data.msg).append($(document.createElement("img")).attr({src: "kep.php?kep=upload/"+data.msg+"&w=180;&h=150;"}));
Does not work, because of the $('#'+data.msg).
param. I don't know how to fix it. I want to make a sub-element to the #test-list and name it to the value of data.msg variable.
-
1
What is the value of
data.msg
and what exactly is not working? Any error messages? – Felix Kling Commented Feb 15, 2010 at 20:26 - 2 Does data.msg change over time? E.g. is this in a loop structure? – Ben Zotto Commented Feb 15, 2010 at 20:26
5 Answers
Reset to default 2You could chain this entire task:
$("<li>")
.appendTo("#test-list")
.attr("id", data.msg)
.append("<img>")
.find("img:first")
.attr("src", "kep.php?kep=upload/" + data.msg + "&w=180;&h=150;");
Which produces the following:
<li id="foo">
<img src="kep.php?kep=upload/foo&w=180;&h=150;">
</li>
Where "foo" was the value of data.msg
.
You are to be mixing DOM calls and jQuery in an odd way. I'd suggest doing it all with jQuery. The chain of elements is somewhat mixed up in terms of what you are appending to, and what you are specifying attributes for. You can specify the id, etc. directly in the creation of an element.
This should work:
$('#test-list').append($("<li id='"+data.msg+"'><img src='kep.php?kep=upload/"+data.msg+"&w=180;&h=150'></li>")
You can append the image as you create the <li>
.
For example:
$('#test-list').append(
$('<li><img src="kep.php?kep=upload/' + data.msg + '&w=180;&h=150" /></li>')
.attr('id', data.msg)
);
Try this out
var img = $("<img>").attr({src: "kep.php?kep=upload/"+data.msg+"&w=180;&h=150;"});
$("<li>").attr({id: data.msg}).append(img).appendTo('#test-list');
If you are using jQuery 1.4.x you can do:
var img = $("<img>",{src: "kep.php?kep=upload/"+data.msg+"&w=180;&h=150;"});
$("<li>",{id: data.msg}).append(img).appendTo('#test-list');
If I had to guess, I'd say that your data.msg has a value that isn't allowed as an ID for selector purposes (generally starting with [-a-zA-Z] and containing [_-\da-zA-Z]. If your image name (data.msg) begins with, or is a number, I'd suggest adding a prefix to the id.
In either case, you can do this in a single statement...
$("<li/>")
.attr("id", data.msg)
.append(
$("<img/>")
.attr("src", "kep.php?kep=upload/"+data.msg+"&w=180;&h=150")
)
.appendTo("#test-list");
本文标签: jqueryJavaScript variable value in elementappendStack Overflow
版权声明:本文标题:jquery - JavaScript variable value in element.append - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741521994a2383243.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论