2 回答

一杯2塊的奶茶
TA貢獻226條經驗 獲得超75個贊
.phcolor{? ???color:#999; }
<input?type="text"?id="uname"?name="uname"?placeholder="請輸入用戶名"/>
<script> ?$(function(){?? ??//判斷瀏覽器是否支持placeholder屬性 ??supportPlaceholder='placeholder'in?document.createElement('input'), ??placeholder=function(input){ ????var?text?=?input.attr('placeholder'), ????defaultValue?=?input.defaultValue; ????if(!defaultValue){ ??????input.val(text).addClass("phcolor"); ????} ????input.focus(function(){ ??????if(input.val()?==?text){ ????????$(this).val(""); ??????} ????}); ????input.blur(function(){ ??????if(input.val()?==?""){ ????????$(this).val(text).addClass("phcolor"); ??????} ????}); ????//輸入的字符不為灰色 ????input.keydown(function(){ ??????$(this).removeClass("phcolor"); ????}); ??}; ??//當瀏覽器不支持placeholder屬性時,調用placeholder函數 ??if(!supportPlaceholder){ ????$('input').each(function(){ ??????text?=?$(this).attr("placeholder"); ??????if($(this).attr("type")?==?"text"){ ????????placeholder($(this)); ??????} ????}); ??} }); </script>
ie10會有點小問題,其他ie沒問題

慕粉3207054
TA貢獻3條經驗 獲得超0個贊
解決IE8的兼容性問題 <script?type="text/javascript">?? ??if(?!('placeholder'?in?document.createElement('input'))?){??? ????$('input[placeholder],textarea[placeholder]').each(function(){??? ??????var?that?=?$(this),??? ??????text=?that.attr('placeholder');??? ??????if(that.val()===""){??? ????????that.val(text).addClass('placeholder');??? ??????}??? ??????that.focus(function(){??? ????????if(that.val()===text){??? ??????????that.val("").removeClass('placeholder');??? ????????}??? ??????})??? ??????.blur(function(){??? ????????if(that.val()===""){??? ??????????that.val(text).addClass('placeholder');??? ????????}??? ??????})??? ??????.closest('form').submit(function(){??? ????????if(that.val()?===?text){??? ??????????that.val('');??? ????????}??? ??????});??? ????});??? ??}?? </script>
- 2 回答
- 1 關注
- 2219 瀏覽
相關問題推薦
添加回答
舉報
0/150
提交
取消