gpt4 book ai didi

.append() 之后的 JQuery Lazyload 图像

转载 作者:行者123 更新时间:2023-12-01 03:45:42 24 4
gpt4 key购买 nike

我正在使用 .append() 将图像加载到 div 中

如何在这些新附加的图像上使用惰性加载器插件?有办法做到吗?

更新:这是代码

//This is a live search script, type something in #livesearch input and it populates a #results ul list with the results
$('#livesearch').live('keyup', function(event) {
//after ke press detected immediately clear the results div of any previous content
$('#results').html("");
// Use the inputed letters to send off an ajax request
$.getJSON("http://xxxxxx.com/live/" + $(this).val(), function(resp){
//the ajax returns a json object, next loop through the object and print out the results in a list using append
$.each(resp.response.docs, function(index, item) {
$('#results').append("<li><div style='background-color:rgb(31,31,31);padding:15px;color:rgb(255,255,255);'><span class='inline' style='width:125px;height:50px;margin-right:15px'><img data-original='img/"+item.nameid+".png' src='img/grey.gif' class='lazy' style='display:inline' height='50px'></span><span class='inline' style='width:300px;'><div style='font-size:14px;'>"+item.name+"</div><div style='font-size:12px;color:rgb(99,99,99)'>Strategy | 4 months ago | Played 2,000 times</div></span></div></li>");
});
});

// The list that was created includes images and could have several hundred of them, therefore I would like to lazy load them as you scroll down
// This line of code is just one of my failed attempts
$("img.lazy").lazyload();
});

因为图像是在文档加载后添加的,我认为惰性加载器插件很难“看到”原始 HTML 中的图像,因为它们在那里不存在。也许 LazyLoad 无法完成我想要的事情。

最佳答案

从 getJSON 回调中调用lazyload()

$.getJSON("http://xxxxxx.com/live/" + $(this).val(), function(resp){
$.each(resp.response.docs, function(index, item) {
$('#results').append("<li>... \
<img data-original='img/"+item.nameid+".png' \
src='img/grey.gif' class='lazy' /> \
...</li>");
});
});
$("img.lazy").lazyload().removeClass("lazy");
});

添加 .removeClass("lazy") 可防止延迟加载多次绑定(bind)到同一元素。

关于.append() 之后的 JQuery Lazyload 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13711115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com