1.下載安裝jquery
可通過下面的方法引入在線版本的js:
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
2.准備一個json格式的文件,后綴可以不是.json
例如下面是result.json的格式
{
"title":"【UI測試結果】-轉轉2017/1/23 14:47",
"starttime":"2017/1/23 15:00 45",
"endtime":"2017/1/23 15:01 42",
"passcount":10,
"failurecount":5,
"resultinfo":[
{
"name":"發布",
"moudle":"Publish",
"pass":"true",
"onecepass":"true",
"log":"true"
},
{
"name":"登錄",
"moudle":"Login",
"pass":"false",
"onecepass":"true",
"log":"asserterrorlog",
"failurereason":{
"errorlog":"asserterror",
"errorimg":"./登錄.jpg"
}
}
]
}
"title":"【UI測試結果】-轉轉2017/1/23 14:47",
"starttime":"2017/1/23 15:00 45",
"endtime":"2017/1/23 15:01 42",
"passcount":10,
"failurecount":5,
"resultinfo":[
{
"name":"發布",
"moudle":"Publish",
"pass":"true",
"onecepass":"true",
"log":"true"
},
{
"name":"登錄",
"moudle":"Login",
"pass":"false",
"onecepass":"true",
"log":"asserterrorlog",
"failurereason":{
"errorlog":"asserterror",
"errorimg":"./登錄.jpg"
}
}
]
}
3.通過$.getJSON獲得Json文件的數據
例如下面的例子:讀取result.json文件的內容,存儲到result變量中,結果是一個json格式
$.getJSON('./result.json',function(result){}
4.通過【$('#元素id').after(html內容);】將html內容添加到定位到的元素后面
元素定位方式
$("#id"):定位到id,
$(“p"):定位到標簽p,其他標簽同理
$(“.class”):定位到class
插入html內容位置:
- append() - 在被選元素的結尾插入內容
- prepend() - 在被選元素的開頭插入內容
- after() - 在被選元素之后插入內容
- before() - 在被選元素之前插入內容
Json數據的操作
JSON對象[key]來讀取內容:result['title’],或者用result.”title"
數組的對象值,可以通過$.each來獲得數據:
$.each(JSON數組對象,function(遍歷索引i,遍歷對象){操作遍歷的對象})
讀取result.json,追加html的代碼如下
(jquery需要寫在<script>標簽內)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
//使用getJSON方法讀取json數據,
//使用getJSON方法讀取json數據,
//注意:info.json可以是不同類型文件,只要其中的數據為json類型即可
$.getJSON('./result.json',function(result){
var html_title='';
var html_resultinfo='';
html_title += '<b>'+result["title"]+'</b>';
$('#resultitle').after(html_title);
$.each(result["resultinfo"],function(i,item){
if(item["pass"]=="true") {
html_resultinfo += '<tr><td>' + item['name'] + '</td>' +
'<td>' + item['moudle'] + '</td>' +
'<td>' + item["pass"] + '</td>' +
'<td>' + item['onecepass'] + '</td>' +
'<td id="' + item['moudle'] + '" class="collapsed" onclick="collapsedisplay(' + item['moudle'] + ')"><u style="color: blue;">展開</u></td></tr>';
html_resultinfo +='<tr id="' + item['moudle'] + 'info" class="collapsedinfo" style="display:none"><td colspan="5">' + item['log'] + '</td></tr>';
}
$('#infotitle').after(html_resultinfo);//after方法:在每個匹配的元素之后插入內容。
});
});
</script>
</HEAD>
<BODY>
<div style="margin-top: 30px">
<div style="font-size: 30px;text-align: center">
<p id="resultitle" ></p>
</div>
</div>
<div id="resultinfo" style="clear: both;padding-top: 30px">
<table style="width: 1080px">
<tr id="infotitle">
<th style="width:360px">用例名稱</th>
<th style="width:200px">模塊名稱</th>
<th style="width:180px">是否成功</th>
<th style="width:180px">一次成功</th>
<th style="width:160px">詳情</th></tr>
</table>
</div>
</div>
</BODY>
</HTML>
<div id="resultinfo" style="clear: both;padding-top: 30px">
<table style="width: 1080px">
<tr id="infotitle">
<th style="width:360px">用例名稱</th>
<th style="width:200px">模塊名稱</th>
<th style="width:180px">是否成功</th>
<th style="width:180px">一次成功</th>
<th style="width:160px">詳情</th></tr>
</table>
</div>
</div>
</BODY>
</HTML>
