JavaScript 是讓網頁「活起來」的程式語言,語法相對親民,是網頁三要素中學習門檻較低的一環。它可以用來做歡迎訊息、時鐘、跑馬燈、表單驗證,也能顯示停留時間等互動效果。
今天的應用範圍早已不限於瀏覽器:透過 Ajax 取後端資料、以 Node.js 寫服務端、用 node-webkit 之類的引擎封裝成桌面應用,甚至 Unity3D 也採用 JavaScript 作為腳本語言。
運算子共有七大類:指派、算術、比較、邏輯、條件(三元)、位元運算與字串運算子。運算子與運算元組成的就是運算式,常見有四類:指派、算術、布林與字串運算式。
程式由語句組成,JS 提供 if~else、指派、switch、while、for、do while、break、continue 等基本流程控制。
特別注意布林判斷:只有 0、-0、null、""、false、undefined、NaN 會被視為 false,其他一律為 true——就連字串 "false" 也是 true。
function 關鍵字定義。return 用來回傳結果,也可以省略(此時回傳 undefined)。JS 的核心能力之一是「基於物件」。一組包含屬性與方法的集合就是物件,例如 document.bgColor = "blue" 就是把文件物件的 bgColor 屬性設為藍色。
使用者操作網頁所產生的動作稱為事件,例如點擊滑鼠產生 onclick、游標移到連結上產生 onmouseover。事件通常搭配事件處理函式使用。
var myVariable = "some value";
涉及計時器、DOM 方法、陣列、內建物件、系統函式與事件註冊。
<script type="text/javascript">
var alldata = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
var alldataarr = alldata.split(",");
var num = alldataarr.length - 1;
var timer;
function start() {
clearInterval(timer);
timer = setInterval('change()', 10);
}
function change() {
document.getElementById("oknum").innerHTML = alldataarr[GetRnd(0, num)];
}
function GetRnd(min, max) {
return min + parseInt(Math.random() * (max - min + 1));
}
function ok() {
clearInterval(timer);
document.getElementById("showresult").value =
document.getElementById("oknum").innerText;
}
</script>
<div id="oknum">請單擊開始</div>
<button onclick="start()" accesskey="s">開始</button>
<button onclick="ok()" accesskey="o">停止</button>
您的選擇是: <input type="text" id="showresult" value="" />
<script type="text/javascript">
window.onload = function () {
setInterval(step, 1000);
};
var num = 1;
function step() {
num = (num < 5) ? num + 1 : 1;
document.getElementById("imgId").src = 'images/' + num + '.jpg';
}
</script>
<img src="images/1.jpg" width="500" height="500" id="imgId" />
<script src="js/jquery-1.8.3.min.js"></script>
<script type="text/JavaScript">
(function ($) {
$.fn.extend({
RollTitle: function (opt, callback) {
if (!opt) var opt = {};
var _this = this;
_this.timer = null;
_this.lineH = _this.find("li:first").height();
_this.line = opt.line ? parseInt(opt.line, 10)
: parseInt(_this.height() / _this.lineH, 10);
_this.speed = opt.speed ? parseInt(opt.speed, 10) : 3000;
_this.timespan = opt.timespan ? parseInt(opt.timespan, 10) : 5000;
if (_this.line == 0) this.line = 1;
_this.upHeight = 0 - _this.line * _this.lineH;
_this.scrollUp = function () {
_this.animate({ marginTop: _this.upHeight }, _this.speed, function () {
for (var i = 1; i <= _this.line; i++) {
_this.find("li:first").appendTo(_this);
}
_this.css({ marginTop: 0 });
});
};
_this.hover(function () {
clearInterval(_this.timer);
}, function () {
_this.timer = setInterval(function () { _this.scrollUp(); }, _this.timespan);
}).mouseout();
}
});
})(jQuery);
</script>
<ul id="RunTopic" style="list-style:none;background:#0ff;border:2px dashed blue;width:200px;">
<li style="color:red">i love you</li>
<li style="color:pink">I LOVE YOU</li>
<li style="color:blue">I LOVE you</li>
<li style="color:green">i LOVE YOU</li>
<li style="color:yellowgreen">I love YOU</li>
</ul>
這個外掛的巧思在於:每次滾動完就把第一個 li 移到最後,讓列表看起來可以無限循環,而 DOM 數量維持不變。
先掌握語法與 DOM 操作,再理解事件與計時器,接著補上物件導向與原型鏈,最後接觸非同步(Promise/async-await)與模組化。基礎打穩之後,無論框架怎麼替換,都能快速上手。