2017-02-20 182 views
1

我正在測試一些時間格式化系統(比如推特發佈旁邊的Twitter有一段時間),所以我設置了一個準系統HTML頁面來運行我的JavaScript,但輸出並未顯示在我的div中。這是爲什麼發生?任何幫助表示讚賞!爲什麼我的輸出不能顯示在我的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

它應該是$( ​​'#應用程序')的innerHTML –

+0

我不使用jQuery我的選擇。我寫我自己在我的JS文件 –

+0

@JayantiLal,但這將是jQuery ....我認爲問題是'#()'不是一個有效的函數名稱看到這裏https://jsfiddle.net/97b6yajk/比較到此https://jsfiddle.net/kjzw0ay7/ – NewToJS

回答

0

不是在JavaScript有效的標識符。閱讀this答案知道什麼是有效的標識符。

示例代碼段:

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"; 
 
}
<div id="app"> 
 
</div>

+0

修復了我的問題,謝謝! –

+0

@AlexisKnox酷,。 :) –

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);
<html> 
 
<head> 
 
    <title>Testing</title> 
 

 
</head> 
 
<body> 
 
    <div id="app"> 
 
    </div> 
 
</body> 
 
</html>

相關問題