简体   繁体   English

根据 AJAX 调用的 JSON 响应,在 JavaScript 中填充大 HTML div

[英]Populating large HTML div in JavaScript, based on JSON response from AJAX call

I currently have a significant amount of HTML inside a div which is generated within an iteration from a GET request to my .net core server.我目前在一个 div 中有大量的 HTML,它是在从 GET 请求到我的 .net 核心服务器的迭代中生成的。

Some of the values in the HTML is dynamic data returned from the server. HTML 中的一些值是从服务器返回的动态数据。

I've added a button to my website, which when pressed, makes an AJAX call to the server to return a JSON object representing the dynamic values for the next iteration.我在我的网站上添加了一个按钮,当按下该按钮时,它会向服务器发出 AJAX 调用,以返回 JSON object 表示下一次迭代的动态值。

So, I would like the JavaScript to add a new div to the DOM, which is the same, and has my dynamic values populated based on the JSON response.因此,我希望 JavaScript 向 DOM 添加一个新的 div,它是相同的,并且根据 JSON 响应填充我的动态值。

For context, the DIV looks like this (which is from a bootstrap template I purchased):对于上下文,DIV 看起来像这样(来自我购买的引导程序模板):

   <div class="card">
        <!-- Card header START -->
        <div class="card-header border-0 pb-0">
            <div class="d-flex align-items-center justify-content-between">
                <div class="d-flex align-items-center">
                    <!-- Avatar -->
                    <div class="avatar me-2">
                        <a href="#!"> <img class="avatar-img rounded-circle" src="@post.ProfileImageUrl" alt="profile picture"> </a>
                    </div>
                    <!-- Info -->
                    <div>
                        <div class="nav nav-divider">
                            <h6 class="nav-item card-title mb-0"> <a href="#!"> @post.ProfileName </a></h6>
                            <span class="nav-item small"> @post.PostTime</span>
                        </div>
                    </div>
                </div>
                <!-- Card feed action dropdown START -->
                <div class="dropdown">
                    <a href="#" class="text-secondary btn btn-secondary-soft-hover py-1 px-2" id="cardFeedAction" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="bi bi-three-dots"></i>
                    </a>
                    <!-- Card feed action dropdown menu -->
                    <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="cardFeedAction">
                        <li><a class="dropdown-item" href="#"> <i class="bi bi-bookmark fa-fw pe-2"></i>Save post</a></li>
                        <li><a class="dropdown-item" href="#"> <i class="bi bi-person-x fa-fw pe-2"></i>Unfollow lori ferguson </a></li>
                        <li><a class="dropdown-item" href="#"> <i class="bi bi-x-circle fa-fw pe-2"></i>Hide post</a></li>
                        <li><a class="dropdown-item" href="#"> <i class="bi bi-slash-circle fa-fw pe-2"></i>Block</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item" href="#"> <i class="bi bi-flag fa-fw pe-2"></i>Report post</a></li>
                    </ul>
                </div>
                <!-- Card feed action dropdown END -->
            </div>
        </div>
        <!-- Card header END -->
        <!-- Card body START -->
        <div class="card-body">
            <p><strong>@post.PostBody</strong></p>
            <!-- Card img -->
            <img class="card-img img-fluid" src="@post.PostImageUrl" alt="Post">
            <!-- Feed react START -->
            <ul class="nav nav-stack py-3 small">
                <li class="nav-item">
                    <a class="nav-link active" href="#!"> <i class="bi bi-hand-thumbs-up-fill pe-1"></i>Liked (56)</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#!"> <i class="bi bi-chat-fill pe-1"></i>Comments (12)</a>
                </li>
                <!-- Card share action START -->
                <li class="nav-item dropdown ms-sm-auto">
                    <a class="nav-link mb-0" href="#" id="cardShareAction" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="bi bi-reply-fill flip-horizontal ps-1"></i>Share (3)
                    </a>
                    <!-- Card share action dropdown menu -->
                    <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="cardShareAction">
                        <li><a class="dropdown-item" href="#"> <i class="bi bi-envelope fa-fw pe-2"></i>Send via Direct Message</a></li>
                        <li><a class="dropdown-item" href="#"> <i class="bi bi-bookmark-check fa-fw pe-2"></i>Bookmark </a></li>
                        <li><a class="dropdown-item" href="#"> <i class="bi bi-link fa-fw pe-2"></i>Copy link to post</a></li>
                        <li><a class="dropdown-item" href="#"> <i class="bi bi-share fa-fw pe-2"></i>Share post via …</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item" href="#"> <i class="bi bi-pencil-square fa-fw pe-2"></i>Share to News Feed</a></li>
                    </ul>
                </li>
                <!-- Card share action END -->
            </ul>
        </div>
        <!-- Card body END -->
        <!-- Card footer START -->
        <div class="card-footer border-0 pt-0">
            <!-- Load more comments -->
            <a href="#!" role="button" class="btn btn-link btn-link-loader btn-sm text-secondary d-flex align-items-center" data-bs-toggle="button" aria-pressed="true">
                <div class="spinner-dots me-2">
                    <span class="spinner-dot"></span>
                    <span class="spinner-dot"></span>
                    <span class="spinner-dot"></span>
                </div>
                Load more comments
            </a>
        </div>
        <!-- Card footer END -->
    </div>
    <!-- Card feed item END -->

Question:问题:

There's a lot of HTML here.这里有很多 HTML。 What's the best way to build this out in Javascript?在 Javascript 中构建它的最佳方法是什么? Is it just a case of biting the bullet and writing all of the JS code to create each element/class?是否只是硬着头皮编写所有 JS 代码来创建每个元素/类?

Would this be the best practice?这是最佳做法吗? Because it effectively means I'm maintaining the HTML markup in two places..因为它实际上意味着我在两个地方维护 HTML 标记。

Thank you..!谢谢..!

I usually save the html of the template to be rendered inside an hidden element.我通常将要呈现的模板的 html 保存在隐藏元素中。 Then I can always access it using that element.innerHTML .然后我总是可以使用那个element.innerHTML访问它。 You could use specialized script tag instead.您可以改用专门的script标签。 But the innerHTML part is the same.innerHTML部分是相同的。

After having the HTML as string you have 2 options将 HTML 作为字符串后,您有 2 个选项

  1. build the string with the values you want to populate, then add that as innerHTML to the target container.使用要填充的值构建字符串,然后将其作为innerHTML添加到目标容器中。
  2. but a better approach is to create an element from that html (see function below) then append it and manipulate it otherwise.但更好的方法是从html (请参阅下面的 function)创建一个元素,然后是 append 并以其他方式对其进行操作。

 // render 5 cards for (var i = 0; i < 5; i++) { var html = document.querySelector("#card-template").innerHTML; var elem = elemFromString(html) // change something elem.querySelector(".card-body>p strong").innerText = "card " + i // append document.querySelector("#container").append(elem) } // usefull utility function elemFromString(html) { var dummy = document.createElement("div"); dummy.innerHTML = html.trim(); if (dummy.children.length > 1) { console.error("expecting one wrapping element for html. will return only firstChild") } var result = dummy.firstChild; result.parentNode.removeChild(result) return result; }
 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous"> <div id="container"> </div> <div id="card-template" style="display:none"> <div class="card"> <.-- Card header START --> <div class="card-header border-0 pb-0"> <div class="d-flex align-items-center justify-content-between"> <div class="d-flex align-items-center"> <.-- Avatar --> <div class="avatar me-2"> <a href="#."> <img class="avatar-img rounded-circle" src="@post.ProfileImageUrl" alt="profile picture"> </a> </div> <.-- Info --> <div> <div class="nav nav-divider"> <h6 class="nav-item card-title mb-0"> <a href="#!"> @post.ProfileName </a></h6> <span class="nav-item small"> @post.PostTime</span> </div> </div> </div> <!-- Card feed action dropdown START --> <div class="dropdown"> <a href="#" class="text-secondary btn btn-secondary-soft-hover py-1 px-2" id="cardFeedAction" data-bs-toggle="dropdown" aria-expanded="false"> <i class="bi bi-three-dots"></i> </a> <!-- Card feed action dropdown menu --> <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="cardFeedAction"> <li> <a class="dropdown-item" href="#"> <i class="bi bi-bookmark fa-fw pe-2"></i>Save post</a> </li> <li> <a class="dropdown-item" href="#"> <i class="bi bi-person-x fa-fw pe-2"></i>Unfollow lori ferguson </a> </li> <li> <a class="dropdown-item" href="#"> <i class="bi bi-x-circle fa-fw pe-2"></i>Hide post</a> </li> <li> <a class="dropdown-item" href="#"> <i class="bi bi-slash-circle fa-fw pe-2"></i>Block</a> </li> <li> <hr class="dropdown-divider"> </li> <li> <a class="dropdown-item" href="#"> <i class="bi bi-flag fa-fw pe-2"></i>Report post</a> </li> </ul> </div> <!-- Card feed action dropdown END --> </div> </div> <!-- Card header END --> <!-- Card body START --> <div class="card-body"> <p><strong>@post.PostBody</strong></p> <!-- Card img --> <img class="card-img img-fluid" src="@post.PostImageUrl" alt="Post"> <!-- Feed react START --> <ul class="nav nav-stack py-3 small"> <li class="nav-item"> <a class="nav-link active" href="#!"> <i class="bi bi-hand-thumbs-up-fill pe-1"></i>Liked (56)</a> </li> <li class="nav-item"> <a class="nav-link" href="#!"> <i class="bi bi-chat-fill pe-1"></i>Comments (12)</a> </li> <!-- Card share action START --> <li class="nav-item dropdown ms-sm-auto"> <a class="nav-link mb-0" href="#" id="cardShareAction" data-bs-toggle="dropdown" aria-expanded="false"> <i class="bi bi-reply-fill flip-horizontal ps-1"></i>Share (3) </a> <!-- Card share action dropdown menu --> <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="cardShareAction"> <li> <a class="dropdown-item" href="#"> <i class="bi bi-envelope fa-fw pe-2"></i>Send via Direct Message</a> </li> <li> <a class="dropdown-item" href="#"> <i class="bi bi-bookmark-check fa-fw pe-2"></i>Bookmark </a> </li> <li> <a class="dropdown-item" href="#"> <i class="bi bi-link fa-fw pe-2"></i>Copy link to post</a> </li> <li> <a class="dropdown-item" href="#"> <i class="bi bi-share fa-fw pe-2"></i>Share post via …</a> </li> <li> <hr class="dropdown-divider"> </li> <li> <a class="dropdown-item" href="#"> <i class="bi bi-pencil-square fa-fw pe-2"></i>Share to News Feed</a> </li> </ul> </li> <!-- Card share action END --> </ul> </div> <!-- Card body END --> <!-- Card footer START --> <div class="card-footer border-0 pt-0"> <!-- Load more comments --> <a href="#!" role="button" class="btn btn-link btn-link-loader btn-sm text-secondary d-flex align-items-center" data-bs-toggle="button" aria-pressed="true"> <div class="spinner-dots me-2"> <span class="spinner-dot"></span> <span class="spinner-dot"></span> <span class="spinner-dot"></span> </div> Load more comments </a> </div> <!-- Card footer END --> </div> <!-- Card feed item END --> </div>

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

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