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"
}
]
结果展示:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。