本次周记的主要内容是关于JQuery JSON 的三个使用实验
JQuery JSON使用实验①
<!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": "Rihanna",
"url": "http://www.3029786859.com",
"slogan": "wakaka!"
},
{
"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使用实验②
<!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":"地址1",
"author":"作者1",
"url":"./img/1.jpeg"
},
{
"name":"大海星",
"address":"地址1",
"author":"作者2",
"url":"./img/2.jpeg"
},
{
"name":"海滨日落",
"address":"地址1",
"author":"作者3",
"url":"./img/3.jpeg"
},
{
"name":"日出",
"address":"地址1",
"author":"作者4",
"url":"./img/4.jpeg"
},
{
"name":"桂林山水甲天下",
"address":"地址1",
"author":"作者5",
"url":"./img/5.jpeg"
},
{
"name":"花田中的木屋",
"address":"地址1",
"author":"作者6",
"url":"./img/6.jpeg"
}
]
运行结果截图
JQuery JSON使用实验③
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title></title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
document.addEventListener('plusready', function(){
//console.log("所有plus api都应该在此事件发生后调用,否则会出现plus is undefined。")
});
$(document).ready(function(){
$("button").click(function(){
var city=$("#city").val()
$.ajax({
type: "GET", //请求方式
url: "http://wthrcdn.etouch.cn/weather_mini?city="+city, //url地址,就是json文件的路径
dataType: "json", //数据类型,可以是 text xml json script jsonp
success: function(result){ //result是响应信息返回的结果,此处包含了返回的json对象
addBox(result.data.forecast); //调用addBox函数,将result数据添加到box容器中
}
});
});
});
function addBox(json_data) {
$.each(json_data, function(index, obj) {
$("#box").append("<div'>" +
"<p>" + obj['date'] + "</p>" +
"<p>" + obj['high'] + "</p>" +
"<p>" + obj['fengli'] + "</p>" +
"<p>" + obj['low'] + "</p>" +
"<p>" + obj['fengxiang'] + "</p>" +
"<p>" + obj['type'] + "</p>" +
"</div>");
});
}
</script>
</head>
<body>
<div>
<h3>请输入城市:</h3>
城市:<input type="text" id="city"/>
<br>
<button type="button">提交</button>
</div>
<br>
<div id="box"></div>
</body>
</html>
运行结果截图