简体   繁体   English

如何防止DIV元素环绕每个P元素?

[英]How do I prevent my DIV element from wrapping around each of my P elements?

I'm pulling some strings out of a couple of arrays of objects and trying to layer them in div elements. 我正在从几个对象数组中拉出一些字符串,然后尝试将它们分层到div元素中。 Here is my code: 这是我的代码:

function renderBlogs() {
    for (i = 0; i < blogArticles.length; i++) {
        var currentArticle = blogArticles[i];

        var divArticleWrapper = document.createElement("div");
        divArticleWrapper.className = "article-wrapper";

        var articleTitle = document.createElement("h1");
        articleTitle.innerHTML = currentArticle.title;

        var articleAuthor = document.createElement("h4");
        articleAuthor.innerHTML = currentArticle.author;

        var articlePublishedOn = document.createElement("h4");
        articlePublishedOn.innerHTML = currentArticle.publishedOn;

        var articleURL = document.createElement("a");
        var articleText = document.createTextNode(currentArticle.url);
        articleURL.appendChild(articleText);
        articleURL.href = currentArticle.url;

        divArticleWrapper.appendChild(articleTitle);
        divArticleWrapper.appendChild(articleAuthor);
        divArticleWrapper.appendChild(articlePublishedOn);
        divArticleWrapper.appendChild(articleURL)
        document.getElementById("blog-container").appendChild(divArticleWrapper);

            for (j = 0; j < currentArticle.content.length; j++) {
                var currentContent = currentArticle.content[j];

                var divContentWrapper = document.createElement("div");
                divContentWrapper.className = "content-wrapper";

                var contentHeading = document.createElement("h2");
                contentHeading.innerHTML = currentContent.heading;

                var contentParagraph = document.createElement("p");
                contentParagraph.innerHTML = currentContent.paragraph;

                divContentWrapper.appendChild(contentHeading);
                divContentWrapper.appendChild(contentParagraph);
                divArticleWrapper.appendChild(divContentWrapper);
            };
    };

This works fine for "article-wrapper", but in "content-wrapper", the div wraps around each individual paragraph element instead of wrapping around all three, like so: 这对于“ article-wrapper”效果很好,但是在“ content-wrapper”中,div环绕每个单独的段落元素,而不是环绕所有三个元素,如下所示:

<div class="content-wrapper">
  <h2>Slow</h2>

  <p>Is changing the DOM slow? What about loading a &lt;script&gt; in the
  &lt;head&gt;? JavaScript animations are slower than CSS ones, right?
  Also, does a 20-millisecond operation take too long? What about 0.5
  seconds? 10 seconds?</p>
</div>

<div class="content-wrapper">
  <p>While it’s true that different operations take different amounts of
  time to complete, it’s hard to say objectively whether something is slow
  or fast without the context of when it’s happening. For example, code
  running during idle time, in a touch handler or in the hot path of a game
  loop each has different performance requirements. Put another way, the
  people using your website or app have different performance expectations
  for each of those contexts. Like every aspect of UX, we build for our
  users, and what they perceive is what matters most. In fact, number one
  on Google’s ten things we know to be true is “Focus on the user and all
  else will follow.”</p>
</div>

<div class="content-wrapper">
  <p>Asking “What does slow mean?,” then, is really the wrong question.
  Instead, we need to ask “What does the user feel when they’re interacting
  with the things we build?”</p>
</div>

I've tinkered around and I'm not quite sure what's wrong. 我已经纠结了,我不太确定出什么问题了。

You are creating multiple divs. 您正在创建多个div。 You need to create content-wrapper outside for and append paragraphs inside the loop. 您需要在for外部创建content-wrapper for并在循环内添加段落。

暂无
暂无

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

相关问题 如何防止我的平滑滚动jquery函数修改div元素的内容? - How do I prevent my smooth-scroll jquery function from modifying the contents of my div elements? 如何防止Markdown将生成的HTML包装在<p>元素中? - How to prevent Markdown from wrapping the generated HTML in a <p> element? 首次加载时,如何防止两个div在网页上跳转? - How do I prevent two divs from jumping around my webpage when it first loads? 如何检查A标签是否环绕IMG元素? - How do I check if an A tag is wrapping around an IMG element? 如何防止我的自定义元素获得自己的 div? - how can I prevent my custom element from getting its own div? 在 CSS 中使用 Overflow-X 时如何防止元素换行? - How do I prevent elements from wrapping while using Overflow-X in CSS? 为什么禁用提交按钮会阻止触发我的提交事件处理程序,我该如何解决? - Why does disabling a submit button prevent my on submit event handler from being triggered, and how do I work around this? 如何防止图形元素到达D3中的图形轴并正确缩放? - How do I prevent graph elements from reaching the axis of my graph in D3 and scale properly? 我的无序列表是从 json 文件呈现的,但我希望每个列表元素都有一个边框 - My unordered list is rendered from a json file but I want a border around each list element 如果内部元素不同,如何从每个循环为数组获取值 - how do i get value to my array from each loop if the inner elements were different
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM