jQuery 全盛時期,幾乎每個專案都會載入它。但隨著瀏覽器標準化,過去必須靠 jQuery 才能優雅完成的事,現在原生 API 都做得到。對於只需一兩個效果的頁面,省下這 30KB 左右的體積與一次額外請求,是很有感的優化。
$('#layer') → document.getElementById('layer')
$('#layer span') → document.querySelectorAll('#layer span')
$('#inner').parent() → document.getElementById('inner').parentNode
$(window).width() → document.documentElement.clientWidth
$('#layer').width() → document.getElementById('layer').offsetWidth
// 新增
$('#wrap').append('<span>a</span>');
var span = document.createElement('span');
span.innerHTML = 'a';
document.getElementById('wrap').appendChild(span);
// 移除(jQuery 的 remove 對應原生 removeChild)
function deleteSpan() {
var content = document.getElementById('wrap');
var childs = content.getElementsByTagName('span');
if (childs.length > 0) {
content.removeChild(childs[childs.length - 1]);
deleteSpan();
}
}
現代瀏覽器已有更簡潔的寫法:el.remove() 直接移除自己,不必找父節點;el.insertAdjacentHTML('beforeend', '<span>a</span>') 一行取代上面四行。
$('#wrap').css({'left': '100px'});
var wrap = document.getElementById('wrap');
wrap.style.left = '100px';
$('#banner').hide(); → document.getElementById('banner').style.display = 'none'
$('#banner').show(); → document.getElementById('banner').style.display = 'block'
$('#people').addClass('people_run2'); → document.getElementById('people').classList.add('people_run2')
$('#people').removeClass('people_run1'); → document.getElementById('people').classList.remove('people_run1')
$('#number').text(1); → document.getElementById('number').textContent = 1
classList 是這裡最值得記的改善。原生加移除 class 過去得自己處理空白字元與重複判斷,現在 add、remove、toggle、contains 全部現成,語意與 jQuery 幾乎一一對應。
$.ajax({
type: "POST",
url: 'run.php',
data: 's=' + last_step,
dataType: "JSON",
timeout: 2000,
success: function (data) { /* 處理回調 */ }
});
原生做法需要手動走完四個步驟:
// 1. 建立 XMLHttpRequest 物件
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
if (xmlhttp.overrideMimeType) { xmlhttp.overrideMimeType('text/xml'); }
} else if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // IE6/IE5
}
if (xmlhttp.upload) {
// 2. 註冊回呼
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4) {
if (xmlhttp.status === 200) {
var json = JSON.parse(xmlhttp.responseText);
// 處理回調
}
}
};
// 3. 設定連線(第三參數 true 為非同步)
xmlhttp.open("POST", "/run.php", true);
// 4. 送出資料
var formdata = new FormData();
formdata.append("s", last_step);
xmlhttp.send(formdata);
}
這段對比最能說明 jQuery 的價值:它把冗長、跨瀏覽器分歧的細節收在一個簡潔 API 之下。但也正因如此,現在你不需要再寫這一段了——原生 fetch 兩行就能取代:
const res = await fetch('/run.php', {
method: 'POST',
body: new FormData(formElement)
});
const json = await res.json();
另外原範例用 eval('(' + xmlhttp.responseText + ')') 解析 JSON,這在今天是明確的安全禁忌(任意程式碼執行)。請一律改用 JSON.parse()。
$('#btn').bind({ 'touchstart': function () {} });
→ document.getElementById('btn').ontouchstart = function () {}
更好的做法是 addEventListener,因為它能綁定多個監聽器、能精確移除、也不會覆蓋既有處理:
document.getElementById('btn')
.addEventListener('touchstart', function () {});
判斷標準很實際:若頁面只是少量 DOM 操作與樣式切換,原生寫法如今更短、更快、也不必多載一個檔案;若專案重度依賴 Ajax、動畫、外掛生態,或團隊成員已習慣 jQuery 思維,繼續使用完全合理。若現有專案想逐步移除,建議從新寫的程式碼開始改用原生 API,而不是發動一次大重構。