实验1
代码
<html>
<head><meta charset="utf-8">
<title>JQuery JSON实例 - 通过JSON对象设置元素属性</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
var JSONObject = [
{
"name": "菜鸟教程",
"url": "http://www.runoob.com",
"slogan": "学的不仅是技术,更是梦想!"
},
{
"name": "W3School",
"url": "http://www.w3school.com.cn",
"slogan": "领先的Web技术教程!"
},
{
"name": "Uni-app",
"url": "https://uniapp.dcloud.io",
"slogan": "为开发者而生,简单易用的跨平台前端框架!"
},
{
"name": "北京工商大学大学",
"url": "http://www.btbu.edu.cn",
"slogan": "开始追逐梦想的地方!"
},
{
"name": "百度",
"url": "http://www.baidu.com",
"slogan": "搜索你所需要的!"
}
];
$("a").each(function(index, element) {
element.innerHTML=JSONObject[index].name;
element.href=JSONObject[index].url;
element.title=JSONObject[index].slogan;
});
})
</script>
</head>
<body>
<a href="#" class="list-group-item active" target="_blank"></a>
<a href="#" class="list-group-item" target="_blank"></a>
<a href="#" class="list-group-item" target="_blank"></a>
<a href="#" class="list-group-item" target="_blank"></a>
<a href="#" class="list-group-item" target="_blank"></a>
</body>
</html>
运行结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210701205035266.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80ODgxMzU1OQ==,size_16,color_FFFFFF,t_70)
实验2
代码
html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<title>JSON示例</title>
<style>
.p {
display: inline-block;
float: left;
width: 50%;
font-family: Microsoft YaHei;
}
.p1 {
font-size: 14px;
color: #000;
margin-top: 16px;
}
.p2 {
font-size: 12px;
color: #b0b0b0;
}
.p3 {
font-size: 14px;
color: #ff5f19;
}
.product {
width: 100%;
position: relative;
margin: 20px 0 5px 0;
height: 100px;
background: #fafafa;
}
.img {
width: 100px;
height: 100px;
float: left;
margin-right: 20px;
}
</style>
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "item.json",
dataType: "json",
success: function(result){
addBox(result);
}
});
function addBox(result) {
$.each(result, function(index, obj) {
$("#box").append("<div class='product'>" +
"<div><img class='img' src='" + obj['url'] + "'></div>" +
"<div class='p1 p'>" + obj['name'] + "</div>" +
"<div class='p2 p'>" + obj['address'] + "</div>" +
"<div class='p3 p'>" + obj['author'] + "</div>" +
"</div>");
});
}
});
</script>
</head>
<body>
<div id="box">
</div>
</body>
</html>