Home  >  Article  >  Web Front-end  >  jQuery implements HTML5 placeholder effect example_jquery

jQuery implements HTML5 placeholder effect example_jquery

WBOY
WBOYOriginal
2016-05-16 16:27:591601browse

You must know the new placeholder attribute in HTML5, right? It doesn’t matter if you don’t know. If the input box has default text, such an effect is often needed. Click to make the default text disappear, and make the default text appear after losing focus.

Today I share a piece of jQuery code to simulate the placeholder effect.

Javascript code:

Copy code The code is as follows:

function placeHolder(event){
var self = $(this), selfDataValue = self.attr("data-value"), selfValue = self.val();
if(selfDataValue){
event.type == "click" ? (selfValue == selfDataValue && (self.val("").css("color","#333"))) : (event.type == "blur" && (selfValue == "" && (self.val(selfDataValue).css("color","#A9A9A9"))))
}else{
Return false;
}
}
$(".pInputText").on("click blur",placeHolder);

Html code:

Copy code The code is as follows:


Some people may ask, since HTML5 provides such functions, why write JS?

Isn’t this nonsense? Of course it’s a compatibility issue. If IE was more compatible with HTML5, who would be willing to use js to implement this thing?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn