我从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手风琴中。
当我点击jsonphp文件时,它的返回是正确的,我想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次,所有值都会发生这种情况。
在您的DOM中,可能有多个元素的classchilds。