在h5的新屬性中,已經可以支持該功能,直接在input里面加上placeholder=“請輸入XXX……”就可以達到效果。
但是低版本的ie和其他不支持h5新屬性的瀏覽器就不能用這個方法了。
另一種簡單的方法就是直接設置value和樣式,value="請輸入XXX……" style="color:#333" 然后添加
onFocus="if(value==defaultValue){value='';this.style.color='#000'}" onBlur="if(!value){value=defaultValue;this.style.color='#999'}" style="color:#999999"
感覺不是很滿意,又找到一種方法:
html
<input type="text" id="name" maxlength="20" fs="請輸入預約人姓名">
JS
//請輸入預約人姓名提示$('#name').val($('#name').attr('fs')).CSS('color','#999');$('#name').focus(function(){if($(this).val()==$(this).attr('fs')){ $(this).val(''); $(this).css('color','#333'); } });$('#name').blur(function(){if($(this).val().trim()==''){ $(this).val($(this).attr('fs')); $(this).css('color','#999'); } });
接著由于業務需求,需要將部分input設為不可編輯,但在ie低版本下發現,鼠標單擊后會出現光標閃爍,于是找了一下,發現有這么一個屬性
unselectable="on" (不可選中的)可以直接達到效果
<input id="PRoject" type="text" readonly="readonly" unselectable="on" fs="可選項目">
新聞熱點
疑難解答