简体   繁体   English

在jQuery / Javascript中生成复杂的HTML元素

[英]Generate complex HTML elements in jQuery/Javascript

I have a scenario where I receive the data from JSON, and that data should be dynamically displayed across the web page, in complex HTML structures (ie tables containing lists etc). 我有一个场景,我从JSON接收数据,并且该数据应以复杂的HTML结构(即包含列表等的表)动态显示在整个网页上。

Any time the JSON data is received, I want my HTML elements to be dynamically regenerated. 每当接收到JSON数据时,我都希望动态地重新生成HTML元素。 For example, HTML table rows count will depend on the data from JSON. 例如,HTML表的行数将取决于JSON中的数据。

Given that the HTML structures are complex, and they should also have some CSS associated to them - what is the best way to do that? 鉴于HTML结构很复杂,并且它们还应该与之关联一些CSS-最好的方法是什么?

My idea was to "declare" single elements in the HTML file, and then putting them all together into a complex structure in Javascript (jQuery). 我的想法是“声明” HTML文件中的单个元素,然后将它们全部组合成Javascript(jQuery)的复杂结构。

For example, if I need to generate a table that has a list in each cell , and every list is dynamically created and filled with elements in javascript, I would then declare the following: 例如,如果我需要生成一个在每个单元格中都有一个列表 ,并且每个列表都是动态创建的,并用javascript中的元素填充,那么我将声明以下内容:

HTML file snippet with declarations 带有声明的HTML文件片段

    <!-- Define table -->
    <script type="text" id="my_table">            
        <table class="a-bordered a-align-center a-spacing-base a-size-base"></table>
    </script>

    <!-- Define table heading element: each column has it's own heading -->
    <script type="text" id="my_table_heading">
        <th class="a-color-base a-size-base a-text-center"></th>
    </script>

    <!-- Define table row -->
    <script type="text" id="my_table_row">
        <tr class="a-spacing-base a-spacing-top-base a-text-center"></tr>
    </script>

    <!-- Define list -->
    <script type="text" id="my_list">
        <ol class="a-ordered-list a-vertical a-spacing-base"></ol>
    </script>

    <!-- Define list item -->
    <script type="text" id="my_list_item">            
        <li class="a-list-normal a-spacing-none a-spacing-top-mini"><span class="a-list-item a-color-link"></li>
    </script>

When I have all that declared, I would use it in jQuery to dynamically create my table with the elements predefined in HTML. 声明所有内容后,我将在jQuery中使用它使用HTML中预定义的元素动态创建表。 Something like the following.. 如下所示。

Javascript file snippet JavaScript文件片段

// get the predefined table heading
var table_heading = $($("#my_table_heading").html());

// append it to the table :: do that in a for loop for all headings
$("#my_table").append(table_heading);

// get the predefined list from HTML
var list = $($("#my_list").html());

// append it to the cell
$("#my_cell").append(list);

My question is that the right approach? 我的问题是正确的方法吗? I am new to Javascript/jQuery/HTML, so I would like to know the best practice how to generate complex html structures in it? 我是Javascript / jQuery / HTML的新手,所以我想知道最佳实践,如何在其中生成复杂的html结构?

  1. Is this OK? 这个可以吗?
  2. Is there any framework that does it better? 有没有更好的框架?

You can use templating libraries like mustache( https://mustache.github.io/ ) or handlebars( http://handlebarsjs.com/ ) . 您可以使用模板库,例如mustache( https://mustache.github.io/ )或handlebars( http://handlebarsjs.com/ )。 They are maintainable and easy to use. 它们易于维护且易于使用。

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

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