想在jsp页面的某个<div>中调用一个获取系统时间的,div的id设置成了time,写了一个js函数通过id调用不成功
展开全部
时间js代码:Clock.js文件
function Clock() {
var date = new Date();
this.year = date.getFullYear();
this.month = date.getMonth() + 1;
this.date = date.getDate();
this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];
this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
this.toString = function() {
return this.year + "年" + this.month + "月" + this.date + "日 " + this.hour + ":" + this.minute + ":" + this.second + " " + this.day;
};
this.toSimpleDate = function() {
return this.year + "-" + this.month + "-" + this.date;
};
this.toDetailDate = function() {
return this.year + "-" + this.month + "-" + this.date + " " + this.hour + ":" + this.minute + ":" + this.second;
};
this.display = function(ele) {
var clock = new Clock();
ele.innerHTML = clock.toString();
window.setTimeout(function() {clock.display(ele);}, 1000);
};
}
需要显示时间的页面
<SCRIPT src="js/Clock.js" type=text/javascript></SCRIPT> //引入Clock.js文件,注意路径
<label id=clock></label> //放入标签,插入时间
<SCRIPT type=text/javascript> //实例化clock对象
var clock = new Clock();
clock.display(document.getElementById("clock"));
</SCRIPT>
function Clock() {
var date = new Date();
this.year = date.getFullYear();
this.month = date.getMonth() + 1;
this.date = date.getDate();
this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];
this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
this.toString = function() {
return this.year + "年" + this.month + "月" + this.date + "日 " + this.hour + ":" + this.minute + ":" + this.second + " " + this.day;
};
this.toSimpleDate = function() {
return this.year + "-" + this.month + "-" + this.date;
};
this.toDetailDate = function() {
return this.year + "-" + this.month + "-" + this.date + " " + this.hour + ":" + this.minute + ":" + this.second;
};
this.display = function(ele) {
var clock = new Clock();
ele.innerHTML = clock.toString();
window.setTimeout(function() {clock.display(ele);}, 1000);
};
}
需要显示时间的页面
<SCRIPT src="js/Clock.js" type=text/javascript></SCRIPT> //引入Clock.js文件,注意路径
<label id=clock></label> //放入标签,插入时间
<SCRIPT type=text/javascript> //实例化clock对象
var clock = new Clock();
clock.display(document.getElementById("clock"));
</SCRIPT>
更多追问追答
追问
现在要放入的jsp页面代码如下:
js应该没有问题,关键是怎么写到页面里,以上试过了,还是不行,输出是空白
追答
别直接把全部代码复制到一个网页中,js代码是一个文件,然后后面另一个页面才引入js文件,我这边运行没问题,年月日 时分秒和星期都能显示。
展开全部
你会不会jquery,如果会的话最好,写起来也方便。
js内容如下:
$(document).ready(functions(){
var d = new Date()
var vYear = d.getFullYear()
var vMon = d.getMonth() + 1
var vDay = d.getDate()
var h = d.getHours();
var m = d.getMinutes();
var se = d.getSeconds();
s=vYear+(vMon<10 ? "0" + vMon : vMon)+(vDay<10 ? "0"+ vDay : vDay)+(h<10 ? "0"+ h : h)+ (m<10 ? "0" + m : m)+(se<10 ? "0" +se : se);
$("#id").html(s);
});
js内容如下:
$(document).ready(functions(){
var d = new Date()
var vYear = d.getFullYear()
var vMon = d.getMonth() + 1
var vDay = d.getDate()
var h = d.getHours();
var m = d.getMinutes();
var se = d.getSeconds();
s=vYear+(vMon<10 ? "0" + vMon : vMon)+(vDay<10 ? "0"+ vDay : vDay)+(h<10 ? "0"+ h : h)+ (m<10 ? "0" + m : m)+(se<10 ? "0" +se : se);
$("#id").html(s);
});
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询