【发布时间】:2010-10-09 20:16:09
【问题描述】:
我在处理 json 请求时将 p 标记附加到 div,并会根据请求中的内容对其进行样式设置。
$(document).ready(function() {
function populatePage() {
var numberOfEntries = 0;
var total = 0;
var retrieveVal = "http://www.reddit.com/" + $("#addressBox").val() + ".json";
$("#redditbox").children().remove();
$.getJSON(retrieveVal, function (json) {
$.each(json.data.children, function () {
title = this.data.title;
url = this.data.url;
ups = this.data.ups;
downs = this.data.downs;
total += (ups - downs);
numberOfEntries += 1;
$("#redditbox").append("<p>" + ups + ":" + downs + " <a href=\"" + url + "\">" + title + "</a><p>");
$("#redditbox :last-child").css('font-size', ups%20); //This is the line in question
});
$("#titlebox h1").append(total/numberOfEntries);
});
}
populatePage()
$(".button").click(function() {
populatePage();
});
});
不幸的是,事情并没有按计划进行。有问题的行的样式适用于 div 的每个孩子,而不仅仅是当时碰巧附加的那个,所以它们最终都具有相同的大小,而不是取决于它们的数量。
如何在 p 标签附加到 div 时将样式应用到它们?
编辑:感谢 Fortes 和 Veggerby 都工作,但我最终选择了 Fortes,因为我做到了。
【问题讨论】:
标签: javascript jquery