下面是之前寫的一個交互的例子,直接拿過來用一用啦,最主要的在於將把https地址作爲變量拼接在link/script 內部,其餘的都是次要的。
示例如下:
時間的json數據格式:data.json
{"commitTime": 1588061853944}
示例代碼:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>jQuery數據結構渲染(2):時間戳的處理</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
時間:<div id="commintTime"></div>
</body>
<script type="text/javascript">
$.ajax({
url: "data.json",
type: 'GET',
dataType: 'json',
success: function(data) {
//
var htm = '';
htm += '' + formatTime(data.commintTime) + '';
$('#commintTime').html(htm)
}
});
//格式化時間,時間戳的處理
function formatTime(commintTime) {
var date = new Date();
//date.setTime(value);
var month = date.getMonth() + 1;
var hours = date.getHours();
if (hours < 10)
hours = "0" + hours;
var minutes = date.getMinutes();
if (minutes < 10)
minutes = "0" + minutes;
var time = date.getFullYear() + "-" + month + "-" + date.getDate() +
" " + hours + ":" + minutes;
return time;
}
</script>
</html>
顯示:
把https地址作爲變量拼接在link/script 內部
主要代碼:
<script type="text/javascript">
var webpath = "https://cdn.bootcss.com"
document.write('<link rel="stylesheet" type="text/css" href="' + webpath +'/twitter-bootstrap/4.3.1/css/bootstrap.min.css">');
document.write('<scr' + 'ipt src="' + webpath + '/jquery/3.4.1/jquery.min.js"></scr' + 'ipt>');
document.write('<scr' + 'ipt src="' + webpath + '/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></scr' + 'ipt>');
</script>
例子:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>jQuery數據結構渲染(2):時間戳的處理</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> -->
</head>
<body>
時間:<div id="commintTime"></div>
</body>
<script type="text/javascript">
var webpath = "https://cdn.bootcss.com"
document.write('<link rel="stylesheet" type="text/css" href="' + webpath +'/twitter-bootstrap/4.3.1/css/bootstrap.min.css">');
document.write('<scr' + 'ipt src="' + webpath + '/jquery/3.4.1/jquery.min.js"></scr' + 'ipt>');
document.write('<scr' + 'ipt src="' + webpath + '/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></scr' + 'ipt>');
</script>
<script type="text/javascript">
$.ajax({
url: "test.json",
type: 'GET',
dataType: 'json',
success: function(data) {
//
var htm = '';
htm += '' + formatTime(data.commintTime) + '';
$('#commintTime').html(htm)
}
});
//格式化時間,時間戳的處理
function formatTime(commintTime) {
var date = new Date();
//date.setTime(value);
var month = date.getMonth() + 1;
var hours = date.getHours();
if (hours < 10)
hours = "0" + hours;
var minutes = date.getMinutes();
if (minutes < 10)
minutes = "0" + minutes;
var time = date.getFullYear() + "-" + month + "-" + date.getDate() +
" " + hours + ":" + minutes;
return time;
}
</script>
</html>
完成
這個時候可以打開F12,查看一下dom元素
可以看到,通過拼接的地址可以打印在dom元素裏面了。