我从stackoverflow中搜索并实现了以下jquery代码,即从php获取json对象并将其作为li标签附加,但是它附加了一个值的3个实例。 示例代码为:
var $childs = $('.childs');
$.getJSON('common/db/fetchUniversity.php', function(data) {
$(data).each(function() {
var $element = $childs.clone().removeClass('childs').appendTo('#colleges');
$element.attr('id', this.id+"u");
$element.html("<a href='#' class='img'><strong><img alt='image' src='images/mit.jpg' style='height:40px; width:40px;' /></strong></a><div style='padding-right: 30px;'><h4 style='display:inline; margin:1px;'>"+this.name+"</h4><p style='display:inline;'>"+this.description+"</p></div><p style='margin:2px;'><a href='#' style='text-decoration: none;'><span class='ui-icon ui-icon-circle-plus' style='display:inline-block; vertical-align:middle;'></span>Follow</a></p><ul class='actions'><li class='remove'><a href='javascript:RemoveMe('#"+this.id+"u')'><span class='ui-icon ui-icon-closethick'>close</span></a></li></ul>");
});
});
$.getJSON('common/db/fetchPeople.php', function(data1) {
$(data1).each(function() {
var $element = $childs.clone().removeClass('childs').appendTo('#people');
$element.attr('id', this.id+"p");
$element.html("<a href='#' class='img'><strong><img alt='image' src='"+btoa(this.pic)+"' style='height:40px; width:40px;' /></strong></a><div style='padding-right: 30px;'><h4 style='display:inline; margin:1px;'>"+this.name+"</h4><p style='display:inline;'>"+this.job_desc+","+this.location+"</p></div><p style='margin:2px;'><a href='#' style='text-decoration: none;'><span class='ui-icon ui-icon-circle-plus' style='display:inline-block; vertical-align:middle;'></span>Follow</a></p><ul class='actions'><li class='remove'><a href='javascript:RemoveMe('#"+this.id+"p')'><span class='ui-icon ui-icon-closethick'>close</span></a></li></ul>");
});
});
$.getJSON('common/db/fetchGroups.php', function(data) {
$(data2).each(function() {
var $element = $childs.clone().removeClass('childs').appendTo('#groups');
$element.attr('id', this.id+"g");
$element.html("<a href='#' class='img'><strong><img alt='image' src='images/groups/hbr.jpg' style='height:40px; width:40px;' /></strong></a><div style='padding-right: 30px;'><h4 style='display:inline; margin:1px;'>"+this.name+"</h4><p style='display:inline;'>"+this.job_desc+","+this.location+"</p></div><p style='margin:2px;'><a href='#' style='text-decoration: none;'><span class='ui-icon ui-icon-circle-plus' style='display:inline-block; vertical-align:middle;'></span>Follow</a></p><ul class='actions'><li class='remove'><a href='javascript:RemoveMe('#"+this.id+"g')'><span class='ui-icon ui-icon-closethick'>close</span></a></li></ul>");
});
});
上面的代码是获取3个json对象,并将其附加到li标签,但以上所有都添加了3个单值实例。 li标签在jquery手风琴里面。
当我点击json php文件时,它返回正确,我猜jquery代码对我做错了。请让我知道我哪里出错了。
json对象,在浏览器中输入时: -
[{"id":1,"name":"Stanford university","description":"One of the best university in the world"},{"id":2,"name":"Princeton University","description":"One of the best university in the world"},{"id":3,"name":"Yale University","description":"One of the best university in the world"},{"id":4,"name":"California University","description":"One of the best university in the world"},{"id":5,"name":"Yale University","description":"One of the best university in the world"},{"id":6,"name":"California University","description":"One of the best university in the world"},{"id":7,"name":"Princeton University","description":"One of the best university in the world"},{"id":8,"name":"Stanford university","description":"One of the best university in the world"},{"id":9,"name":"California University","description":"One of the best university in the world"},{"id":10,"name":"Princeton University","description":"One of the best university in the world"},{"id":11,"name":"Yale University","description":"One of the best university in the world"}]
以上id:1值追加3次,所有值都会发生。
答案 0 :(得分:1)
也许DOM中有多个带有 childs 类的元素。