這份筆記把日常開發中最常回頭查的細節整理成八個部分,從圖文對齊、跨瀏覽器 hack,到 DOM 操作與數字處理。內容偏工具書性質,建議當作參考隨時翻閱,而不必一次讀完。
<li>記住密碼<img src="" align="bottom" style="margin-bottom:-4px" /></li>
<li>記住密碼<img src="static/img/xyx.jpg" align="middle" /></li>
補充:align 屬性已於 HTML5 中廢棄,建議改用 vertical-align: middle / text-bottom 搭配 line-height 來處理。
<script language="javascript">
setTimeout('top.moveTo(0,0)',5000);
setTimeout('top.resizeTo(screen.availWidth,screen.availHeight)',5000);
</script>
註:現代瀏覽器基於安全與使用者體驗考量,已全面禁止腳本自行改變視窗大小,這段程式碼目前只在少數舊版 IE 有效。
用 window.open() 開啟的子視窗,可以透過 window.opener 取得開啟它的父視窗。
window.opener.test(); // 呼叫父視窗的 test()
if (window.opener && !window.opener.closed) {
window.opener.document.parentForm.stockBox.value = symbol;
}
history.go(0)
location.reload()
location = location
location.assign(location)
document.execCommand('Refresh')
window.navigate(location)
location.replace(location)
document.URL = location.href
自動重新整理與自動跳轉可用 meta:
<meta http-equiv="refresh" content="20"> <meta http-equiv="refresh" content="20;url=https://example.com">
也可用 setTimeout 週期觸發 window.location.reload()。框架環境下的刷新寫法:
parent.location.reload(); // 刷新含此框架的頁面 self.opener.location.reload(); // 子視窗刷新父視窗 parent.另一FrameID.location.reload(); // 刷新另一個框架
條件註釋可以精準指定 IE 版本:
<!--[if !IE]> 除 IE 外都可識別 <![endif]--> <!--[if IE]> 所有 IE 可識別 <![endif]--> <!--[if IE 5.0]> 只有 IE5.0 可識別 <![endif]-->
三種常見的 CSS hack 寫法:
/* 第一種:!important 與星號 */
.div { background:orange; *background:green !important; *background:blue; }
/* 第二種:星號與下底線 */
.div { margin:10px; *margin:15px; _margin:15px; }
/* 第三種:html 選擇器 */
#div { color: #333; }
*+html #div { color: #999; }
* html #div { color: #666; }
提醒:用下底線做類名分隔符(如 _style)雖然是常見 hack,但並不合規,CSS 檢查工具會報錯,實務上請用連字符 -。
<a> 生效,需改用 JavaScript 或 table 替代方案。height:1%、zoom:1 或指定寬高來觸發 hasLayout 解決。display:inline,並用 margin-right:-3px 抵銷 3px 間隙。overflow:hidden、zoom:0.8、line-height:1px。font-size 設為 0;或將 img 設為 vertical-align:bottom / margin-bottom:-5px。由低到高:元素與偽元素選擇器 [0.0.0.1] → 類別/屬性/偽類選擇器 [0.0.1.0] → ID 選擇器 [0.1.0.0] → 行內樣式 [1.0.0.0]。!important 會凌駕上述全部。
word-wrap:break-word; overflow:hidden;display:block,或 vertical-align 設為 top/bottom/middle/text-bottom;也可將父容器 font-size 設為 0。white-space:nowrap; text-overflow:ellipsis; overflow:hidden;line-height 等於容器高度。vertical-align:middle。<param name="wmode" value="transparent" /> 或 opaque。
#exm {
position: absolute;
left: 50%;
top: 50%;
z-index: 1;
width: 200px;
height: 100px;
margin-left: -100px;
margin-top: -52px;
}
註:這是把元素左上角定位到畫面中心後,再用負 margin 拉回一半尺寸。現今更簡潔的做法是 top:50%; left:50%; transform:translate(-50%,-50%),或直接 display:flex; justify-content:center; align-items:center。
border:0 是把寬度設為 0 像素,瀏覽器仍會進行 border-width/border-color 的渲染計算;border:none 則是宣告「沒有邊框」,解析時不做渲染。但在 IE6/IE7 的 button 與 input 上,none 反而可能失效,此時 0 較可靠;若要 none 全相容,可在同一選擇器上補一個 background 屬性。
.e { padding-bottom: 32767px; margin-bottom: -32767px; }
用一個極大的 padding 撐高,再用等量的負 margin 把它對外的高度抵銷,讓所有欄位視覺上等高。這個技巧歷史悠久,現在用 display:flex 或 CSS Grid 一行就能取代。
<div id="test"><span>test1</span>test2</div> test.innerText → test1 test2 test.innerHTML → <span>test1</span>test2 test.outerHTML → <div id="test"><span>test1</span>test2</div>
a = 23.50abc typeof(a) → "string" parseFloat(a) → 23.5 parseInt(a) → 23 Number(a) → NaN
Number() 遇到任何非數字字元即回傳 NaN;parseInt() 由左至右盡量轉換,遇到第一個非數字就停;isNaN() 用來判斷結果是否為非數字。
nodeType 1 = 元素節點,3 = 文本節點
nodeName 節點名稱,文本節點為 #text
nodeValue 節點的值
obj.firstChild = obj.childNodes[0]
obj.lastChild = obj.childNodes[obj.childNodes.length - 1]
obj.hasChildNodes() // 是否有子節點
var parentElm = myLinkItem.parentNode;
while (parentElm && parentElm.className != 'syna' && parentElm != document.body) {
parentElm = parentElm.parentNode;
}
修改屬性有兩種方式:直接當作文物屬性存取(mainImage.src = ''),或用 getAttribute() / setAttribute()。
// 指定小數位數
function roundTo(base, precision) {
var m = Math.pow(10, precision);
return Math.round(base * m) / m;
}
roundTo(3.942487, 3); // 3.942
// 範圍內的隨機整數
function randomBetween(min, max) {
return min + Math.floor(Math.random() * (max - min + 1));
}
// 轉字串
var a = 10;
a = String(a); // 或 a = a.toString();
// 取得函式宣告的參數個數
function add(n1, n2) {}
add.length; // 2
// 取得實際傳入的參數個數
function add(n1, n2) { return arguments.length; }
補充:變數命名可用數字、字母、$ 與底線,但不可數字開頭;getElementsByTagName() 需等文件載入完成後才能取到正確結果。
.element-class {
display: -moz-inline-stack; /* Firefox 舊版 */
display: inline-block;
zoom: 1; /* IE */
*display: inline; /* IE hack */
}
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.clearfix { zoom: 1; }
製作一個 16×16 像素的 .ico 檔放在網站根目錄,多數瀏覽器會自動抓取;若要明確指定:
<link rel="icon" href="https://example.com/favicon.ico" />
div {
width: 200px;
height: 100px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
body {
scrollbar-face-color: #f6f6f6;
scrollbar-highlight-color: #fff;
scrollbar-shadow-color: #eeeeee;
scrollbar-3dlight-color: #eeeeee;
scrollbar-arrow-color: #000;
scrollbar-track-color: #fff;
scrollbar-darkshadow-color: #fff;
}
<ul class="list"> <li><span>2006年6月6日</span><a href="#">新聞標題</a></li> <li><span>2006年6月6日</span><a href="#">新聞標題</a></li> </ul>
這份清單裡有相當比例是 IE6/IE7 時代的產物。如果今天才開始的新專案,IE 相關的 hack、expression、私有滾動條屬性、Flash 疊層設定都可以直接跳過;但盒子模型、優先級、L-V-H-A、clearfix、省略號截斷這些觀念完全不會過時,值得確實理解而非背誦。