Home >Web Front-end >JS Tutorial >jQuery simple method to achieve gray prompt text effect in input text box_jquery
The example in this article describes how jQuery simply implements the gray prompt text effect in the input text box. Share it with everyone for your reference, the details are as follows:
$(function(){ $(".grayTips").each(function(){ //遍历每个文本框 var objTextBox=$(this); var oldText=$.trim(objTextBox.val()); objTextBox.css("color","#888"); objTextBox.focus(function(){ if(objTextBox.val()!=oldText){ objTextBox.css("color","#000"); } else{ objTextBox.val("").css("color","#888"); } }); objTextBox.blur(function(){ if(objTextBox.val()==""){ objTextBox.val(oldText).css("color","#888"); } }); objTextBox.keydown(function(){ objTextBox.css("color","#000"); }); }); });
Note: jquery.js file needs to be introduced. Both input and TextBox are valid
$(function(){ $(".grayTip").each(function(){ var $input=$(this); $input.css("color","#888"); var oldText=$.trim($input.val()); $input.focus(function(){ var newText=$.trim($input.val()); if (newText==oldText){ $input.val(""); } $input.css("color","#000"); }); $input.blur(function(){ var newText=$.trim($input.val()); if(newText==""){ $input.val(oldText); $input.css("color","#888"); } }); }); });
I hope this article will be helpful to everyone in jQuery programming.