.phcolor{ color:#999;}//css樣式css
function isPlaceholer(){
var input = document.createElement("input");jquery
return "placeholder" in input;
};
function placeholder(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(!isPlaceholer()){
$('input').each(function(){
text = $(this).attr("placeholder");
if($(this).attr("type") == "text"){
placeholder($(this));
}
});
}//結束瀏覽器
也有可用jquery-placeholder.js插件解決。但插件字體顏色不是灰色。函數