編程(Programming)是編定程序的中文簡稱,就是讓計算機代碼解決某個問題,對某個計算體系規定一定的運算方式,使計算體系按照該計算方式運行,并最終得到相應結果的過程。為了使計算機能夠理解(understand)人的意圖,人類就必須將需解決的問題的思路、方法和手段通過計算機能夠理解的形式告訴計算機,使得計算機能夠根據人的指令一步一步去工作,完成某種特定的任務。這種人和計算體系之間交流的過程就是編程。 【實例名稱】 帶鏈接的滾動字幕 【實例描述】 滾動文本是新聞顯示的重要手段,如果要在滾動的文本中實現鏈接,則需要動態設置鏈接文本和鏈接地址。本例學習如何實現帶鏈接的滾動字幕。 【實例代碼】 <html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>標題頁-本站(www.xue51.com)</title>
</head>
<body>
<script language="JavaScript1.2">
var marqueewidth=400 //設置marquee的寬度 (in pixels)
var marqueeheight=20 //設置marquee的高度 (in pixels, 該參數只適用于Netscape)
var speed=4 //設置marquee滾動的速度(數值越大速度越快)
//設置marquee顯示內容,使用標準的HTML語法。
var marqueecontents='<strong><big>歡迎支持中國搜索引擎
<a href="http://www.baidu.com">百度一下</a> 找到自己想找的 信息</big></strong></font>'
if (document.all)
document.write('<marquee scrollAmount='+speed+'
style="width:'+marqueewidth+'">'+marqueecontents+'</marquee>')
function regenerate(){
window.location.reload();
//重新加載頁面
}
function regenerate2(){
if (document.layers){
setTimeout("window.onresize=regenerate",450);
//窗體改變大小時重載
intializemarquee();
}
}
function intializemarquee(){
//使用nobr控制顯示的字符個數
document.cmarquee01.document.cmarquee02.document.
write('<nobr>'+marqueecontents+'</nobr>');
document.cmarquee01.document.cmarquee02.document.close();
thelength=document.cmarquee01.document.
cmarquee02.document.width; //獲取層的寬度
scrollit();
//實現字體的滾動
}
function scrollit(){
if (document.cmarquee01.document.cmarquee02
.left>=thelength*(-1)){
document.cmarquee01.document.cmarquee02.left-=speed;
setTimeout("scrollit()",100);
//定時器實現不停的調用
}
else{
document.cmarquee01.document.cmarquee02.left=marqueewidth;
scrollit();
}
}
window.onload=regenerate2;
</script>
<ilayer width=&{marqueewidth}; height=&{marqueeheight};
name="cmarquee01">
<layer name="cmarquee02"></layer>
</ilayer>
</body>
</html>
【運行效果】 
【難點剖析】 本例的重點主要包括如何動態添加鏈接和如何實現文本的滾動=代碼中使用了一個全局變量“marqueecontents來保存鏈接內容和地址:文本的滾動通過定時器不斷地調用“scrollit”方法實現。 【源碼下載】 如果你不愿復制代碼及提高代碼準確性,你可以點擊:帶鏈接的滾動字幕 進行本實例源碼下載
使用編程語言寫的程序,由于每條指令都對應計算機一個特定的基本動作,所以程序占用內存少、執行效率高。 |