如何在JS函数中编写多个HTML div

时间:2018-08-31 15:44:18

标签: javascript html

所以我想在页面上打印一个对象,但我不知道如何在JS中编写代码,以便在页面上为每个对象打印多个div。

我要插入的内容

<div class="cv-block">
        <div id="parent_div_1">
            obiect.firstn
            obiect.lastn
        </div>

        <div id="parent_div_2">
            obiect.date
        </div>

        obiect.message

    </div>

进入此Java脚本功能

function afisare (lista) {
var randuri = "";
lista.forEach(function (obiect) {

    randuri += ;
});
$("#obiect").html(randuri);}

提前感谢您的时间。

1 个答案:

答案 0 :(得分:0)

执行此操作的最佳方法是使用ES6支持的模板文字。 如果要在较旧的浏览器中使用它,则应使用babel。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAXLEN 100

    struct Element
       {
        char one[100];
        char two[100];
        char three[100];
        int st;
       };

    void insertionSort(struct Element elements[]);
    void printArray(struct Element elements[]);


    int main() {


        int i;
        struct Element elements[MAXLEN];


        FILE * fpointer = fopen("clients.txt", "r");

        char buffer[1024]; // Define a really big string to hold the line of text in
        char *field;
        int field_number;


        while(fgets(buffer,1024,fpointer))
            {
            field_number=0;
            field=strtok(buffer,";");
            while(field)
              {
               switch(field_number)
                {
                  case 0:
                  strcpy(elements[i].one,field);
                  break;
                  case 1:
                  strcpy(elements[i].two,field);
                  break;
                  case 2:
                  strcpy(elements[i].three,field);
                  break;
                  case 3:
                  elements[i].st=atoi(field);
                  break;
                }
             field=strtok(NULL,";"); // Get next field
             field_number++;
             }
           i++; // Move the index for elements to the next one
          }


        insertionSort(elements);
        printArray(elements);

        fclose(fpointer);
        return 0;

    }


void insertionSort(struct Element elements[])
{
   int i, j;
  struct Element key;
   for (i = 1; i < 10; i++)
    {
       key = elements[i];
       j = i-1;


       while (j >= 0 && elements[j].st < key.st)
       {
           elements[j+1] = elements[j];
           j = j-1;
       }
       elements[j+1] = key;
    }

}


    void printArray(struct Element elements[])
    {
    int i;
    for (i=0; i < 3; i++) {
        printf("%s %s %s%d ", elements[i].one,elements[i].two,elements[i].three,elements[i].st);
        printf("\n");
      }
    }

还有ES5代码。

$("#obiect").html(`<div class="cv-block">
        <div id="parent_div_1">
            ${obiect.firstn}
            ${obiect.lastn}
        </div>

        <div id="parent_div_2">
            ${obiect.date}
        </div>

        ${obiect.message}

    </div>`)