$('#submit').on('click',function(e){
var $textVal = $('#textVal').val();
var $listItems = $('.listItems');
var timeAdded = e.timeStamp;
$listItems.prepend('<li>' + $textVal + ' added at ' + timeAdded + '</li>');
$('#textVal').val(' ');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<ol class="listItems">
</ol>
</div>
<input type="textarea" placeholder="What to do?" id="textVal">
<input type="submit" value="To do!" id="submit">
&#13;
按下待办事项添加待办事项列表项时!按钮,添加项目和添加的日期。我尝试使用e.timeStamp
,但它显示了从1970年1月1日到事件触发时的毫秒数。我试图转换它,但我失败了。我该如何转换它以便获得列表项添加的确切时间和日期?
谢谢
答案 0 :(得分:2)
event.timeStamp
应该等于new Date().getTime()
。目前还没有。我的建议是忽略事件timeStamp并使用一个公共Date对象。
$('#submit').on('click',function(e){
var $textVal = $('#textVal').val();
var $listItems = $('.listItems');
var dateAdded = new Date();
$listItems.prepend('<li>' + $textVal + ' added at ' + dateAdded + '</li>');
$('#textVal').val(' ');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<ol class="listItems">
</ol>
</div>
<input type="textarea" placeholder="What to do?" id="textVal">
<input type="submit" value="To do!" id="submit">
&#13;
答案 1 :(得分:1)
使用javascript日期对象
$('#submit').on('click', function(e) {
var $textVal = $('#textVal').val();
var $listItems = $('.listItems');
var timeAdded = new Date;
$listItems.prepend('<li>' + $textVal + ' added at ' + (timeAdded.getDay() + '-'+ (timeAdded.getMonth() + 1) + '-' + timeAdded.getFullYear() + " " + timeAdded.getHours() + ":" + timeAdded.getMinutes() ) + '</li>');
$('#textVal').val(' ');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<ol class="listItems"></ol>
</div>
<input type="textarea" placeholder="What to do?" id="textVal">
<input type="submit" value="To do!" id="submit">
答案 2 :(得分:0)
您可以使用时刻库和
var date = moment(timeAdded)