傳統做法:value 存放提示文字,onfocus 清空,onblur 恢復。現代瀏覽器原生 placeholder 屬性完美替代。
function txtFocus(el){
if(el.defaultValue === el.value){
el.value = '';
el.style.color = '#000';
}
}
function txtBlur(el){
if(el.value === ''){
el.value = el.defaultValue;
el.style.color = '#666';
}
}
// ASP.NET 控件綁定
<asp:TextBox ID="txtBookPerson" CssClass="text2" style="color:#666"
onfocus="txtFocus(this)" onblur="txtBlur(this)" value="請輸入預定人" />
<input type="text" placeholder="請輸入預定人" class="text2" />
::placeholder 偽元素自定義:::placeholder { color: #999; font-style: italic; }
if(!('placeholder' in document.createElement('input'))){
// 載入 jQuery Placeholder 插件或自行掛載 focus/blur 邏輯
}
placeholder,語義化、零代碼、性能最優。title 或提示組件。placeholder 值(原生不會提交),舊 JS 方案需手動判斷 el.value === el.defaultValue 避免提交提示詞。