简体   繁体   English

如何按顺序显示div中的段落和图像?

[英]How to display paragraphs and images from div in order?

I am building a small web-tool where editors can write content by using buttons to add paragraphs and images.我正在构建一个小型网络工具,编辑人员可以通过使用按钮添加段落和图像来编写内容。 I store the elements with an id ((number of element) starting at 0 and incremented for every new element) and load with a button in order to a div "preview" where the content is supposed to be displayed as in the web page later on.我存储带有 id 的元素((元素数量)从 0 开始并为每个新元素递增)并加载一个按钮以进行 div“预览”,内容应该在稍后显示在网页中在。

My issue is that, for a reason I don't understand, the image is always displayed below all the paragraphs instead of being in order.我的问题是,出于我不明白的原因,图像总是显示在所有段落下方,而不是按顺序显示。 Presumably there is an easy fix, but I am very new to HTML, CSS and JS and couldn't find the solution online.大概有一个简单的解决方法,但我对 HTML、CSS 和 JS 非常陌生,无法在线找到解决方案。 Sorry if this is a stupid mistake or the solution was already posted somewhere.抱歉,如果这是一个愚蠢的错误,或者解决方案已经发布在某处。

Javascript handling the preview rendering:处理预览渲染的 Javascript:

// Preview current document status
document.getElementById("previewButton").addEventListener("click", function() {
   // Clear
   document.getElementById("preview").innerHTML = "";

   // Add all elements properly
   var section = document.getElementById("preview");
   var id = "preview";

   for (var counter = 0; counter < element_counter; counter++) {
      var type = document.getElementById(counter).nodeName;

      // If text element
      if (type === "TEXTAREA") {
         var paragraph = document.createElement("p");
         var text = document.getElementById(counter).value;
         paragraph.setAttribute("id", id + counter);
         paragraph.setAttribute("class", "flow-text");
         paragraph.append(text);
         section.appendChild(paragraph);
      }

      // If image element
      if (type === "INPUT") {
         var file = document.getElementById(counter).files[0];
         var reader = new FileReader();
         reader.onload = function(e) {
            var image = document.createElement("img");
            image.setAttribute("id", id + counter);
            image.setAttribute("src", e.target.result);
            image.setAttribute("class", "materialboxed responsive-img");
            section.appendChild(image);
         }
         reader.readAsDataURL(file);
      }
   }
});

在此处输入图片说明

This might work.这可能会奏效。 I can't test though without your code.没有你的代码我无法测试。 However basically the principle at work is to isolate some of the vars so they represent distinct instantiations.然而,工作的基本原则是隔离一些变量,以便它们代表不同的实例。 And then immediately add the image element to the DOM.然后立即将图像元素添加到 DOM。 The reader.onload is expected to run asynchronously still. reader.onload预计仍会异步运行。

enter code here if (type === "INPUT") {

    (function() {
        var file = document.getElementById(counter).files[0];
        var reader = new FileReader();
        var image = document.createElement("img");
        image.setAttribute("id", id + counter);
        image.setAttribute("class", "materialboxed responsive-img");
        section.appendChild(image);

        reader.onload = function(e) {
            image.setAttribute("src", e.target.result);
        }

        reader.readAsDataURL(file);
    }());
}

Javascript - 将照片中的图像显示为唯一的<div></div><div id="text_translate"><p>我已经在这个网站上搜索了几天寻找解决方案。 我想使用 javascript 从我服务器上的文件夹中获取图像并将它们放入这样的集合中。 我认为这会相当简单,但我什么也没想到。</p><p> 它将取代此代码中部分标记之间的下面的 div。</p><p> 我可以对它们全部进行硬编码,但是我需要在这个轮播中显示 107 张图像,从长远来看,能够从文件夹中添加/删除图像比硬编码要容易得多商场。 以下是我到目前为止的代码。</p><pre> &lt;section id="vendors" class="zerogrid"&gt; &lt;div width="90%" &gt; &lt;div class="container" width="90%" style="background-color: #858585;"&gt; &lt;h2 class="clr-1" style="padding: 2%;" &gt;Our Suppliers&lt;/h2&gt; &lt;section class="customer-logos slider" style="padding-bottom: 5%;"&gt; &lt;div class="slide"&gt;&lt;img src="images/image1.png"&gt;&lt;/div&gt; &lt;div class="slide"&gt;&lt;img src="images/image2.png"&gt;&lt;/div&gt; &lt;div class="slide"&gt;&lt;img src="images/image3.png"&gt;&lt;/div&gt; &lt;/section&gt; &lt;/div&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ $('.customer-logos').slick({ slidesToShow: 6, slidesToScroll: 1, autoplay: true, autoplaySpeed: 1000, arrows: false, dots: false, pauseOnHover: true, responsive: [{ breakpoint: 768, settings: { slidesToShow: 106 } }, { breakpoint: 520, settings: { slidesToShow: 3 } }] }); }); &lt;/script&gt; &lt;/section&gt;</pre><p> 这个旋转木马工作得很好,任何帮助得到排序的东西都将不胜感激。</p></div> - Javascript - Display images from photo into unique <div>

暂无
暂无

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

相关问题 如何使用javascript以随机顺序显示文件夹中的图像? - How can I display images from a folder in random order with javascript? 如何以随机顺序显示 JavaScript 数组中的图像 - How to display images from a JavaScript array in random order 如何使用Java脚本从数据库检索图像并在div中显示 - how to retrieve images from database and display in div using java script 如何在 ES6/React 的段落中显示来自 2 个数组的值 - how to display values from 2 arrays in paragraphs in ES6/React Javascript - 将照片中的图像显示为唯一的<div></div><div id="text_translate"><p>我已经在这个网站上搜索了几天寻找解决方案。 我想使用 javascript 从我服务器上的文件夹中获取图像并将它们放入这样的集合中。 我认为这会相当简单,但我什么也没想到。</p><p> 它将取代此代码中部分标记之间的下面的 div。</p><p> 我可以对它们全部进行硬编码,但是我需要在这个轮播中显示 107 张图像,从长远来看,能够从文件夹中添加/删除图像比硬编码要容易得多商场。 以下是我到目前为止的代码。</p><pre> &lt;section id="vendors" class="zerogrid"&gt; &lt;div width="90%" &gt; &lt;div class="container" width="90%" style="background-color: #858585;"&gt; &lt;h2 class="clr-1" style="padding: 2%;" &gt;Our Suppliers&lt;/h2&gt; &lt;section class="customer-logos slider" style="padding-bottom: 5%;"&gt; &lt;div class="slide"&gt;&lt;img src="images/image1.png"&gt;&lt;/div&gt; &lt;div class="slide"&gt;&lt;img src="images/image2.png"&gt;&lt;/div&gt; &lt;div class="slide"&gt;&lt;img src="images/image3.png"&gt;&lt;/div&gt; &lt;/section&gt; &lt;/div&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ $('.customer-logos').slick({ slidesToShow: 6, slidesToScroll: 1, autoplay: true, autoplaySpeed: 1000, arrows: false, dots: false, pauseOnHover: true, responsive: [{ breakpoint: 768, settings: { slidesToShow: 106 } }, { breakpoint: 520, settings: { slidesToShow: 3 } }] }); }); &lt;/script&gt; &lt;/section&gt;</pre><p> 这个旋转木马工作得很好,任何帮助得到排序的东西都将不胜感激。</p></div> - Javascript - Display images from photo into unique <div> 如何设置间隔以便轮流显示图像 - how to set interval in order to display images by turn 如何使用JavaScript以随机顺序显示图像 - How to display images in a random order using javascript JS-从段落中删除所有图像和格式 - JS - Remove all images and format from paragraphs 如何将缓存图像显示为html div对象 - How to display cache images into html div object 如何在可编辑的div中拖放段落? - How to drag and drop paragraphs in a contenteditable div?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM