jQuery$get()-将加载的数据与每个()进行比较
我有两个列表。相册和收藏夹jQuery$get()-将加载的数据与每个()进行比较,jquery,get,compare,each,Jquery,Get,Compare,Each,我有两个列表。相册和收藏夹 首先,我使用这个each()循环将.album项与.favorites项进行比较 当.album列表中有收藏副本时,我会附加一个收藏图标 $(".album li").each(function(index) { var itemSrc = $(this).find("img").attr("src"); if ( $(".favorites li img[src*='" + itemSrc + "']").length > 0 ) {
首先,我使用这个
each()
循环将.album项与.favorites项进行比较当.album列表中有收藏副本时,我会附加一个收藏图标
$(".album li").each(function(index) {
var itemSrc = $(this).find("img").attr("src");
if ( $(".favorites li img[src*='" + itemSrc + "']").length > 0 ) {
$(this).append('<span class="favorite"><\/span>');
}
});
ajax-load-favorites.php返回以下内容:
<li><img src="http://mysite.com/album/tn/006.jpg" /></li>
<li><img src="http://mysite.com/album/tn/003.jpg" /></li>
<li><img src="http://mysite.com/album/tn/010.jpg" /></li>
$.get(“ajax加载收藏夹.php”,{},函数(数据){
//控制台日志(数据);
$(“li”,数据)。每个(函数(索引){
var itemSrc=$(this.find(“img”).attr(“src”);
如果($(“.favorites li img[src*=”+itemSrc+“]”))。长度>0){
$(此)。附加(“”);
}
});
});
基本上是一样的,只需修改选择器以搜索ajax加载的数据。简单的字符串搜索可以解决您的问题:
$.get("ajax-load-favorites.php", {}, function(data) {
// console.log(data);
$(".album li").each(function(index) {
var itemSrc = $(this).find("img").attr("src");
if (data.indexOf(itemSrc) != -1) {
$(this).append('<span class="favorite"><\/span>');
}
});
});
$.get(“ajax加载收藏夹.php”,{},函数(数据){
//控制台日志(数据);
$(“.album li”)。每个(函数(索引){
var itemSrc=$(this.find(“img”).attr(“src”);
if(data.indexOf(itemSrc)!=-1){
$(此)。附加(“”);
}
});
});
您面临的上述代码中的问题是什么,但我的数据中没有- 元素。。。只有列表项
$.get("ajax-load-favorites.php", {}, function(data) {
// console.log(data);
$("li", data).each(function(index) {
var itemSrc = $(this).find("img").attr("src");
if ( $(".favorites li img[src*='" + itemSrc + "']").length > 0 ) {
$(this).append('<span class="favorite"><\/span>');
}
});
});
$.get("ajax-load-favorites.php", {}, function(data) {
// console.log(data);
$(".album li").each(function(index) {
var itemSrc = $(this).find("img").attr("src");
if (data.indexOf(itemSrc) != -1) {
$(this).append('<span class="favorite"><\/span>');
}
});
});