首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

怎么把这个html中的js代码分离成.js文件

2013-01-23 
如何把这个html中的js代码分离成.js文件p aligncenterfont size6 face黑体b2013年情人节/b/

如何把这个html中的js代码分离成.js文件
<p align=center><font size=6 face="黑体"><b>2013年情人节</b></font></p>
<script language=JavaScript> 
<!--
var now = new Date();             

function currenttime() 

var dtime = new Date("2/14/2013");    
now.setTime(now.getTime()+100); 
days = (dtime - now) / 1000 / 60 / 60 / 24;    
daysRound = Math.floor(days); 
hours = (dtime - now) / 1000 / 60 / 60 - (24 * daysRound);    
hoursRound = Math.floor(hours); 
minutes = (dtime - now) / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound);    
minutesRound = Math.floor(minutes); 
if(minutesRound<=9)
minutesRound="0"+minutesRound
seconds = (dtime - now) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
secondsRound = Math.floor(seconds); 
if(secondsRound<=9)
secondsRound="0"+secondsRound
time.innerHTML = daysRound + "天" +hoursRound + "小时" + minutesRound + "分" + secondsRound + "秒";    
}
setInterval("currenttime()",100); 
//-->
</script> 
<p align=center>距离2013年2月14日还有<span id="time"></span></p>


[解决办法]


(function(w){
if(w.BindDate)return;
w.BindDate=function(dom)
{
if(dom.Timer)return;
dom.Timer= setInterval(function(){SetDate(dom)},100);
}
var SetDate=function(dom){

var dtime = new Date("2/14/2013");    
now.setTime(now.getTime()+100); 
days = (dtime - now) / 1000 / 60 / 60 / 24;    
daysRound = Math.floor(days); 
hours = (dtime - now) / 1000 / 60 / 60 - (24 * daysRound);    
hoursRound = Math.floor(hours); 
minutes = (dtime - now) / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound);    
minutesRound = Math.floor(minutes); 
if(minutesRound<=9)
minutesRound="0"+minutesRound
seconds = (dtime - now) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
secondsRound = Math.floor(seconds); 
if(secondsRound<=9)
secondsRound="0"+secondsRound
dom.innerHTML = daysRound + "天" +hoursRound + "小时" + minutesRound + "分" + secondsRound + "秒";    
}
})(window)


[解决办法]
新建一个文本文件,拷贝js 代码,另存为.js 文件就可以。


------解决方案--------------------


<p align=center><font size=6 face="黑体"><b>2013年情人节</b></font></p>
<p align=center>距离2013年2月14日还有<span id="time"></span></p>
<script src="name.js"></script>



name.js
var now = new Date();
function currenttime()
{
var dtime = new Date("2/14/2013");    
now.setTime(now.getTime()+100); 
days = (dtime - now) / 1000 / 60 / 60 / 24;    
daysRound = Math.floor(days); 
hours = (dtime - now) / 1000 / 60 / 60 - (24 * daysRound);    
hoursRound = Math.floor(hours); 
minutes = (dtime - now) / 1000 /60 - (24 * 60 * daysRound) - (60 * hoursRound);    
minutesRound = Math.floor(minutes); 
if(minutesRound<=9)
minutesRound="0"+minutesRound
seconds = (dtime - now) / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
secondsRound = Math.floor(seconds); 
if(secondsRound<=9)
secondsRound="0"+secondsRound
time.innerHTML = daysRound + "天" +hoursRound + "小时" + minutesRound + "分" + secondsRound + "秒";    
}
setInterval("currenttime()",100);

热点排行