ie9 placeholder兼容


 

.phcolor{ color:#999;}//css樣式

function isPlaceholer(){
var input = document.createElement("input");

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插件解決。但插件字體顏色不是灰色。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM