简体   繁体   English

JQuery 在页面加载时替换多个 div 内容

[英]JQuery to replace multiple div contents on page load

I have a series of DIVs created from a PHP loop, each with the same class ('image'), that are generated with a small placeholder image to speed up the initial page load.我有一系列从 PHP 循环创建的 DIV,每个都有相同的 class(“图像”),它们是用一个小的占位符图像生成的,以加速初始页面加载。 However, once the page has finished loading I want to replace each of the divs content with the returned content from another page.但是,一旦页面加载完成,我想用另一个页面返回的内容替换每个 div 内容。 This other page would render a higher quality image, unique to each DIV, and some associated metadata about the image.这个其他页面将呈现更高质量的图像,每个 DIV 都是唯一的,以及一些关于图像的相关元数据。 Essentially it would be a sort of lazy-loading.从本质上讲,这将是一种延迟加载。

After the PHP loop my HTML code looks like the below:在 PHP 循环之后,我的 HTML 代码如下所示:

<div id='content'>
    <h1>Some title goes here</h1>
    <div id='home'>
        <div class='image' id='aaa'><img src='/placeholder.jpg'/></div>
        <div class='image' id='bbb'><img src='/placeholder.jpg'/></div>
        <div class='image' id='ccc'><img src='/placeholder.jpg'/></div>
        <div class='image' id='ddd'><img src='/placeholder.jpg'/></div>
        <div class='image' id='eee'><img src='/placeholder.jpg'/></div>
        <div class='image' id='fff'><img src='/placeholder.jpg'/></div>
    </div>    
</div>

I'm very new to JQuery and had hoped to do something like the below to then replace the contents of each DIV with the class 'image'.我是 JQuery 的新手,我希望做类似下面的事情,然后用 class“图像”替换每个 DIV 的内容。 Essentially the idea is to take the id value from the DIVs with class 'image', parse that to the newimage.php script and then have the HTML returned from newimage.php replace the DIV content.本质上,想法是从带有 class 'image' 的 DIV 中获取 id 值,将其解析为 newimage.php 脚本,然后从 newimage.php 返回的 HTML 替换 DIV 内容。

<script>
    var updateDivs = function(){
        var objid = $(this).attr('id');
        $("#"+objid).load("newimage.php?id="+objid);
    }
    $('.image').ready(updateDivs);
</script>

Such that after the running of the JQuery the HTML code would look like the below:这样在运行 JQuery 之后,HTML 代码将如下所示:

<div id='content'>
    <h1>Some title goes here</h1>
    <div id='home'>
        <div class='image' id='aaa'><span class='image-title>Image Title 1</span><span class='image-views'>10</span><img src='/img/aaa.jpg'/></div>
        <div class='image' id='bbb'><span class='image-title>Image Title 2</span><span class='image-views'>11</span><img src='/img/bbb.jpg'/></div>
        <div class='image' id='ccc'><span class='image-title>Image Title 3</span><span class='image-views'>9</span><img src='/img/ccc.jpg'/></div>
        <div class='image' id='ddd'><span class='image-title>Image Title 4</span><span class='image-views'>8</span><img src='/img/ddd.jpg'/></div>
        <div class='image' id='eee'><span class='image-title>Image Title 5</span><span class='image-views'>12</span><img src='/img/eee.jpg'/></div>
        <div class='image' id='fff'><span class='image-title>Image Title 6</span><span class='image-views'>14</span><img src='/img/fff.jpg'/></div>
    </div>    
</div>

However, after trying a few variations in JQuery code I cannot get this to work as expected.但是,在 JQuery 代码中尝试了一些变体后,我无法让它按预期工作。 Any help would be greatly appreciated!任何帮助将不胜感激!

Your issue is with $(".image").ready(.. - a quick debug shows that this inside the function is the document , not the image. .ready() doesn't work this way.您的问题是$(".image").ready(.. - 快速调试显示 function 中的thisdocument ,而不是图像。.ready .ready()无法以这种方式工作。

.ready() 。准备好()

https://api.jquery.com/ready/ https://api.jquery.com/ready/

Specify a function to execute when the DOM is fully loaded.指定一个 function 在 DOM 完全加载时执行。

The above page shows $("img").ready(handler) and states that this is equivalent to $(handler) .上面的页面显示了$("img").ready(handler)并声明这等同于$(handler) So .ready could be called .documentIsReady .所以.ready可以称为.documentIsReady

There doesn't seem to be a need to wait for your your placeholder to load, so you can just make your call on a normal doc.ready, passing in each image:似乎不需要等待您的占位符加载,因此您只需调用普通的 doc.ready,传入每个图像:

$(() => $('.image').each(updateDivs);

or, if you prefer:或者,如果您愿意:

$(function() {
   $(".image").each(updateDivs)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM