jQuery JSON
JQuery JSON使用实验(1)
预期效果:
点击页面列表中每一项,可以跳转到相应网站。
<!DOCTYPE html>
<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) {
//通过设置DOM对象属性的方式设置<a>标签属性
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>
结果展示:
JQuery JSON使用实验(2)
预期效果:
<!DOCTYPE html>
<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 type="text/css">
.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", //url地址,就是json文件的路径
dataType: "json", //数据类型,可以是 text xml json script jsonp
success: function(result){ //result是响应信息返回的结果,此处包含了返回的json对象
addBox(result); //调用addBox函数,将result数据添加到box容器中
}
});
function addBox(result) {
//result是Json对象的集合,通过JQuery的each方法进行遍历,为每个对象生成div元素
$.each(result, function(index, obj) {
//添加一个class为product的容器,放置产品信息
$("#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>
json代码:
[
{
"name":"血月",
"address":"塞尔达",
"author":"Esther",
"url":"./pic/xueyue.jpg"
},
{
"name":"国王",
"address":"塞尔达",
"author":"Esther",
"url":"./pic/king.jpg "
},
{
"name":"双人卡丁车",
"address":"马里奥赛车8",
"author":"Esther 马",
"url":"./pic/saiche.jpg"
},
{
"name":"建筑物",
"address":"GRIS",
"author":"Esther",
"url":"./pic/jianzhu.png"
},
{
"name":"鸟",
"address":"GRIS",
"author":"Esther",
"url":"./pic/bird.png"
},
{
"name":"休闲娱乐",
"address":"动物森友会",
"author":"Esther",
"url":"./pic/dongsen.jpg"
}
]
结果展示: