一下代码 是我在网上找到的一段关于动态始终的源代码,但是小弟才学JAVA不久,对JS的封装所知甚少,请问这段代码写到JS文件里面去,或者写成一个JS的方法,应该怎么写,格式应该怎么样,在JSP页面又如何调用呢?<body>
<div><font color=black>欢迎登陆问卷调查系统:今天是</font>
<script language="JavaScript" type="text/JavaScript">
<!--
tmpDate = new Date();
date = tmpDate.getDate();
month= tmpDate.getMonth() + 1;
year= tmpDate.getYear();
document.write(year);
document.write("年");
document.write(month);
document.write("月");
document.write(date);
document.write("日 ");
myArray=new Array(6);
myArray[0]="星期日"
myArray[1]="星期一"
myArray[2]="星期二"
myArray[3]="星期三"
myArray[4]="星期四"
myArray[5]="星期五"
myArray[6]="星期六"
weekday=tmpDate.getDay();
if (weekday==0 | weekday==6)
{
document.write(myArray[weekday])
}
else
{document.write(myArray[weekday])
};
// -->
</script>
<span id="liveclock" style"=width: 109px; height: 15px"></span>
<SCRIPT language=javascript>
function www_helpor_net()
{
var Digital=new Date()
var hours=Digital.getHours()
var minutes=Digital.getMinutes()
var seconds=Digital.getSeconds()if(minutes<=9)
minutes="0"+minutes
if(seconds<=9)
seconds="0"+seconds
myclock="现在时刻:<font size='4' face='Arial black'>"+hours+":"+minutes+":"+seconds+"</font>"
if(document.layers){document.layers.liveclock.document.write(myclock)
document.layers.liveclock.document.close()
}else if(document.all)
liveclock.innerHTML=myclock
setTimeout("www_helpor_net()",1000)
}
www_helpor_net();
//-->
</SCRIPT>
</body>
<div><font color=black>欢迎登陆问卷调查系统:今天是</font>
<script language="JavaScript" type="text/JavaScript">
<!--
tmpDate = new Date();
date = tmpDate.getDate();
month= tmpDate.getMonth() + 1;
year= tmpDate.getYear();
document.write(year);
document.write("年");
document.write(month);
document.write("月");
document.write(date);
document.write("日 ");
myArray=new Array(6);
myArray[0]="星期日"
myArray[1]="星期一"
myArray[2]="星期二"
myArray[3]="星期三"
myArray[4]="星期四"
myArray[5]="星期五"
myArray[6]="星期六"
weekday=tmpDate.getDay();
if (weekday==0 | weekday==6)
{
document.write(myArray[weekday])
}
else
{document.write(myArray[weekday])
};
// -->
</script>
<span id="liveclock" style"=width: 109px; height: 15px"></span>
<SCRIPT language=javascript>
function www_helpor_net()
{
var Digital=new Date()
var hours=Digital.getHours()
var minutes=Digital.getMinutes()
var seconds=Digital.getSeconds()if(minutes<=9)
minutes="0"+minutes
if(seconds<=9)
seconds="0"+seconds
myclock="现在时刻:<font size='4' face='Arial black'>"+hours+":"+minutes+":"+seconds+"</font>"
if(document.layers){document.layers.liveclock.document.write(myclock)
document.layers.liveclock.document.close()
}else if(document.all)
liveclock.innerHTML=myclock
setTimeout("www_helpor_net()",1000)
}
www_helpor_net();
//-->
</SCRIPT>
</body>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<script type="text/javascript">
<!--
var $ = function(id){ return document.getElementById(id)};
function getDate(t){
with(t)return [getFullYear(),'年'
,('0'+(getMonth()+1)).slice(-2),'月'
,('0'+getDate()).slice(-2),'日 '
,('星期' + '日一二三四五六'.charAt(getDay())+' ')
,('0'+getHours()).slice(-2),': '
,('0'+getMinutes()).slice(-2),': '
,('0'+getSeconds()).slice(-2)].join('');
} window.onload = function(){
setInterval(function(){
$("ospanTime").innerHTML = getDate(new Date());
}, 1000);
}
//-->
</script>
</head> <body>
<div style="width:300px;height:40px;line-height:40px;background-color:#C0C0C0;font-size:12px;border:2px solid #808080">
当前时间:<span id="ospanTime" style="color:#FF0000;font-weight:bold"></span><br/>
</div>
</body>
</html>
date.js文件 放这些代码 var $ = function(id){ return document.getElementById(id)};
function getDate(t){
with(t)return [getFullYear(),'年'
,('0'+(getMonth()+1)).slice(-2),'月'
,('0'+getDate()).slice(-2),'日 '
,('星期' + '日一二三四五六'.charAt(getDay())+' ')
,('0'+getHours()).slice(-2),': '
,('0'+getMinutes()).slice(-2),': '
,('0'+getSeconds()).slice(-2)].join('');
} window.onload = function(){
setInterval(function(){
$("ospanTime").innerHTML = getDate(new Date());
}, 1000);
} 然后在页面上引用<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<script src="date.js"></script><!-- 这个就是你要引用的js文件的路径-->
</head> <body>
<div style="width:300px;height:40px;line-height:40px;background-color:#C0C0C0;font-size:12px;border:2px solid #808080">
当前时间:<span id="ospanTime" style="color:#FF0000;font-weight:bold"></span><br/>
</div>
</body>
</html>
<body>
<div> <font color=black>欢迎登陆问卷调查系统:今天是 </font>
<script language="JavaScript" type="text/JavaScript">
<!--
function getDt(div_id){
var tmpDate = new Date();
var date = tmpDate.getDate();
var month= tmpDate.getMonth() + 1;
var year= tmpDate.getYear();
var s = year+"年"+month+"月"+date+"日 ";
var myArray=new Array(6);
myArray[0]="星期日"
myArray[1]="星期一"
myArray[2]="星期二"
myArray[3]="星期三"
myArray[4]="星期四"
myArray[5]="星期五"
myArray[6]="星期六"
weekday=tmpDate.getDay();
if (weekday==0 | weekday==6)
{
s+=myArray[weekday]
}
else
{s+=myArray[weekday]
};
document.getElementById(div_id).innerHTML = s;
}
function www_helpor_net(div_id)
{
var Digital=new Date()
var hours=Digital.getHours()
var minutes=Digital.getMinutes()
var seconds=Digital.getSeconds()
if(hours==0 && minutes==0 && seconds==0) location.reload();
if(minutes <=9) minutes="0"+minutes
if(seconds <=9) seconds="0"+seconds
myclock="现在时刻: <font size='4' face='Arial black'>"+hours+":"+minutes+":"+seconds+" </font>"
document.getElementById(div_id).innerHTML=myclock;
setTimeout("www_helpor_net('" + div_id + "')",1000);
} window.onload=function(){
//调用,传标签id
getDt("dt_pad");
www_helpor_net("liveclock");
}
// -->
</script>
<span id="dt_pad"></span><span id="liveclock" style"=width: 109px; height: 15px"> </span>
</body>