【问题标题】:How to show images on the DOM as they come in using Jquery after the DOM has loaded?如何在 DOM 加载后使用 Jquery 在 DOM 上显示图像?
【发布时间】:2021-10-21 16:16:47
【问题描述】:

我正在尝试使用 jquery 向 DOM 添加模板。我等待所有图像加载到 DOM 上,然后抓取特定 div 中的所有图像,将模板添加到 DOM(这是一个两卡网格),然后使用 .each 循环所有这些图像和使用模板附加它们以创建多个两个卡片网格。

问题是它没有显示在 DOM 上。

下面是代码:

 <script>
     function moveImages() {
       var $gallery = $('#gallery');
       // The template below will only have a 2 card grid. Hence an img counter.
       var imgcount = 1;
       // Initializing section counter for the 2 card grid. When img counter = 2, we increment it below so that we can have a new section ID to 
       // append the next two images one by one.
         var sectioncount = 0;
  
         //Here is the HTMl template, as the imgcount crosses 2, we create a new section. notice the dynamic variable below for sectioncount.
         var template = `<section class="card-grid-section section-${sectioncount}" loading="lazy">
         <a class="card-grid-two-image">
       <div>
         <picture id="card-one-two-grid-section-${sectioncount}">
         </picture>
       </div>
     </a>
     <a class="card-grid-two-image">
       <div>
         <picture id="card-two-two-grid-section-${sectioncount}">
         </picture>
       </div>
     </a>
     </section>`;
     
     //getting the html for the article-text id above.
       var test = $('#article-text').find('p').each(function(index, value) {
  
         //looping over all the children of each p tag below which are mostly span and img
         var children = value.childNodes;
         children.forEach(function(item, index1){
     
           // if the tag is a span, then we append it to the div with id gallery.
           if(item.tagName == "SPAN"){
             $gallery.append(`<p><span>${item.innerHTML}<span></p>`);
             console.log("ADDED SPAN TO GALLERY");
           }
  
           //if tag is an img
           else if(item.tagName == "IMG"){
             //I want to skip the first image here
             if(imgcount > 0){
             if(imgcount == 1){
               //appending the template to gallery only if imagecount = 1.
              $gallery.append(template);
               //item here, is an image. I am adding the first image to the card one of the first grid(template). Notice the sectioncount dynamic variable below.
               var $card1 = $(`#card-one-two-grid-section-${sectioncount}`);
               //Appending image to card one
               $card1.append(item);
             } else if(imgcount == 2){
               //if image count is 2, we are adding the the second image to template we created above.
               var $card2 = $(`#card-two-two-grid-section-${sectioncount}`);
               $card2.append(item);
               //Since image count is 2, we want a new grid id. Hence the sectioncount++.
                sectioncount++;
                //since we can only add two images to grid. I am setting imgcount back to 0;
                imgcount = 0;
             } 
             }
             //Incrementing img count below.
             imgcount++;
             }
           });
     });
     
       }
     
     $(document).ready(function($) {
     });
  </script>

【问题讨论】:

  • 你能不能也用html创建一个sn-p
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: javascript jquery shopify


【解决方案1】:

我认为你的意思是这样写:

function gridTemplate(section) {
  return $(`<section class="card-grid-section section-${section}" loading="lazy">
    <a class="card-grid-two-image">
      <div>
        <picture id="card-one-two-grid-section-${section}"></picture>
      </div>
    </a>
    <a class="card-grid-two-image">
      <div>
        <picture id="card-two-two-grid-section-${section}"></picture>
      </div>
    </a>
  </section>`);
}
  
$(function () {
  var imgcount = 1, sectioncount = 0;

  $('#article-text p > *').each(function () {
    if (this.tagName === "SPAN") {
      $('#gallery').append(`<p><span>${this.innerHTML}<span></p>`);
    } else if (this.tagName === "IMG") {
      if (imgcount == 1) {
        $('#gallery').append(gridTemplate(sectioncount));
        $(`#card-one-two-grid-section-${sectioncount}`).append(this);
        imgcount++;
      } else {
        $(`#card-two-two-grid-section-${sectioncount}`).append(this);
        sectioncount++;
        imgcount--;
      }
    }
  });
});

(你真的应该减少那些准确说出代码所说的内容的 cmets - 它们非常分散注意力。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多