javascript - 使用文本和 span 标签创建动态 li 标签

标签 javascript jquery html json ajax

我有一个 JSON 数组,如下所示:

var teamDetails=[ 
 { "pType" : "Search Engines", "count" : 5},
 { "pType" : "Content Server", "count" : 1},
 { "pType" : "Search Engines", "count" : 1},
 { "pType" : "Business", "count" : 1,},
 { "pType" : "Internet Services", "count" : 1},
];

我想创建包含 JSON 数据的动态 ul 和 li 标签

我希望数据像这样放置:

<ul class="list-unstyled">
<li>
    ***Here the json ptype value***
    <span class="pull-right">
        <strong>
            <h5>***here json count value***</h5>
        </strong>
    </span>
</li>
</ul>

如何为每个数据动态获取此输出..?

我尝试使用此代码,但无法弄清楚如何附加文本 li 和 span 标记?

var clist=$('ul.list-unstyled')

dataProjectCount.forEach(function(a){
  a.count=a.count+" Projects";
  console.log(a.count);

  var li=$('<li/>').appendTo(clist);
  var aa=$('<span/>').addClass('pull-right').appendTo(clist);
});

最佳答案

创建映射输入 teamDetails 数组项的 li 元素,然后将它们附加到 ul 容器

const teamDetails=[ 
 { "pType" : "Search Engines", "count" : 5},
 { "pType" : "Content Server", "count" : 1},
 { "pType" : "Search Engines", "count" : 1},
 { "pType" : "Business", "count" : 1,},
 { "pType" : "Internet Services", "count" : 1},
];

const liElements = teamDetails.map(el => $(`<li>${el.pType}<span class="pull-right><strong><h5>${el.count}</h5></strong></span></li>`));
$('.list-unstyled').append(liElements);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="list-unstyled">
</ul>

关于javascript - 使用文本和 span 标签创建动态 li 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54027255/

相关文章:

javascript - 在 NodeJS 中使用变换/双工流

javascript - 滚动时, Logo 颜色发生变化,但向上滚动时它保持不变

javascript - 如何随机翻转 Div 的后面然后再前面

javascript - 使用 javascript/jquery 获取具有图像数组的对象中的所有图像

html - 删除图像下方的空白区域

javascript - 仅在某些环境下 Vue 中的字符串长度无效 RangeError

php - 如何通过 URL 接受 GET 参数,但不包含 ".php?argument=value"

javascript - 使用 jQuery 的 SPAN 内容条件

html - div 什么时候填充水平空间,什么时候不填充?

javascript - 如果多个元素使用相同的自定义验证指令,如何验证表单?