jq循环后台传递的List对象并且将其中的对象属性值附着在前台html标签中做拼接
假设后台传递的List对象为list,其中的对象属性为name,你可以使用jq的each方法循环list,然后将name属性值拼接在前台的html标签中。
示例代码如下:
<!DOCTYPE html>
<html>
<head>
<title>拼接属性值</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</head>
<body>
<div id="result"></div>
<script>
// 假设后台传递的List对象为list
var list = [
{ name: "John" },
{ name: "Jane" },
{ name: "Bob" }
];
$(document).ready(function() {
var result = "";
$.each(list, function(index, item) {
result += "<p>" + item.name + "</p>";
});
$("#result").html(result);
});
</script>
</body>
</html>
上述代码会将list中每个对象的name属性值拼接在<p>标签中,并将结果显示在id为result的div中。
注意:你需要在html中引入jQuery库。可以通过<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>引入
原文地址: https://www.cveoy.top/t/topic/hZ2k 著作权归作者所有。请勿转载和采集!