简体   繁体   English

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

[英]How to write multiple HTML divs in a JS function

So I wanna print an object to my page and I have no idea how to write the code in JS so that on my page it prints multiple divs for each object.所以我想在我的页面上打印一个对象,但我不知道如何在 JS 中编写代码,以便在我的页面上为每个对象打印多个 div。

What I wanna do is insert this我想做的是插入这个

<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>

into this java script function进入这个java脚本函数

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

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

Thank you in advance for your time.提前感谢您的时间。

best way to do this is using template literal which is supported by ES6.最好的方法是使用 ES6 支持的模板文字。 if you want to use it in older browser you should use babel.如果你想在旧浏览器中使用它,你应该使用 babel。

 $("#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>`)

also there is ES5 code for it.还有它的 ES5 代码。

 $("#object").html("<div class=\\"cv-block\\">\\n <div id=\\"parent_div_1\\">\\n obiect.firstn\\n obiect.lastn\\n </div>\\n\\n <div id=\\"parent_div_2\\">\\n obiect.date\\n </div>\\n\\n obiect.message\\n\\n </div>");

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

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