我正在测试一些时间的格式化系统(比如Twitter有时间发布在推文旁边)所以我设置了一个准系统HTML页面来运行我的javascript,但输出没有显示在我的div中。为什么会这样?任何帮助表示赞赏!
页:
<html>
<head>
<title>Testing</title>
</head>
<body>
<div id="app">
<script>
var app = require('ago');
#('app').innerHTML = timeSince(old_date);
</script>
</div>
</body>
</html>
脚本:
var old_date = new Date("November 13, 2017 5:00 PM");
function #(sel) {
return document.getElementById(sel);
}
function timeSince(post_minute) {
var seconds = Math.floor((new Date() - post_minute) / 1000);
var interval = Math.floor(seconds / 31536000);
if (interval > 1) {
return interval + "y";
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + "mo.";
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + "d";
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + "h";
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + "m";
}
return Math.floor(seconds) + " seconds";
}
答案 0 :(得分:0)
#
不是JavaScript中的有效标识符。我将其更改为$
,您的代码运行得很好。
var old_date = new Date("November 13, 2017 5:00 PM");
function $(sel) {
return document.getElementById(sel);
}
function timeSince(post_minute) {
var seconds = Math.floor((new Date() - post_minute) / 1000);
var interval = Math.floor(seconds / 31536000);
if (interval > 1) {
return interval + "y";
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + "mo.";
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + "d";
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + "h";
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + "m";
}
return Math.floor(seconds) + " seconds";
}
$('app').innerHTML = timeSince(old_date);
&#13;
<html>
<head>
<title>Testing</title>
</head>
<body>
<div id="app">
</div>
</body>
</html>
&#13;
答案 1 :(得分:0)
#不是javascript中的有效标识符。阅读this回答以了解有效标识符是什么。
示例代码段:
[...]
self.mylistofprocesses.append(process)
status = {QProcess.NotRunning: "Not Running",
QProcess.Starting: "Starting",
QProcess.Running: "Running"}
process.stateChanged.connect(lambda state: status_item.setText(status[state]))
&#13;
var old_date = new Date("November 13, 2017 5:00 PM");
$('app').innerHTML = timeSince(old_date);
function $(sel) {
return document.getElementById(sel);
}
function timeSince(post_minute) {
var seconds = Math.floor((new Date() - post_minute) / 1000);
var interval = Math.floor(seconds / 31536000);
if (interval > 1) {
return interval + "y";
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + "mo.";
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + "d";
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + "h";
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + "m";
}
return Math.floor(seconds) + " seconds";
}
&#13;