Html
    Css
    Js
1
<input type="text" id="newInput">
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
* {
margin:0;
padding:0;
border:0;
}
*,*:after,*:before {
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
box-sizing:border-box;
}
#newInput {
display:block;
margin:20px auto;
width:500px;
height:50px;
border:2px solid #ccc;
padding:0 20px;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(function($) {
$.fn.setTip = function(options) {
var defaults = {
defaultValue: "",
focusColor: "#000000",
blurColor: "#ccc",
fontSize: "10pt"
}
var options = $.extend(defaults, options);
this.each(function() {
var thisSearch = $(this);
thisSearch.focus(function() {
if (thisSearch.val() == options.defaultValue) {
thisSearch.css("color", options.focusColor);
thisSearch.val("");
}
}).blur(function() {
if (thisSearch.val() === undefined || thisSearch.val() == '') {
thisSearch.css("color", options.blurColor);
thisSearch.val(options.defaultValue);
}
}).css({
"color": options.blurColor,
"font-size": options.fontSize
});
if (thisSearch.val() == 'undefined' || thisSearch.val() == "") {
thisSearch.val(options.defaultValue);
} else if (thisSearch.val() != options.defaultValue) {
thisSearch.css("color", options.focusColor);
}
});
};
})(jQuery);
$(document).ready(function() {
$("#newInput").setTip({
defaultValue: ""
});
});
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
↑上面代码改变,会自动显示代码结果 jQuery调用版本:1.8.3
 立即下载

输入框默认提示文字不使用Placeholder属性

0