获取焦点时移除placeholder,失去焦点重新获得placeholder内容
有个很简单的方法
input:focus::-webkit-input-placeholder{
color: transparent;
}
/* transparent是全透明黑色(black)的速记法,即一个类似rgba(0,0,0,0)这样的值 */
var arrplaceholder = [1,1,1,1];
$("input").focus(function(){
if(!!!$(this).prop("readonly")){
$(this).prop("placeholder", "")
}
});
$("input").blur(function(){
$(this).prop("placeholder", arrplaceholder[$(this).index()])
});
<body>
<input id="test" placeholder="请输入"></input>
</body>
<script type="text/javascript">
test.addEventListener('focus',function(){
test.removeAttribute('placeholder')
});
test.addEventListener('blur',function(){
test.setAttribute('placeholder','请输入')
});
</script>
原生JS...
我很早很早之前写了一个方法:
(function($) {
$.fn.extend({
placeholder: function() {
if ("placeholder" in document.createElement("input")) {
return this //如果原生支持placeholder属性,则返回对象本身
} else {
return this.each(function() {
var _this = $(this);
_this.val(_this.attr("placeholder")).focus(function() {
if (_this.val() === _this.attr("placeholder")) {
_this.val("")
}
}).blur(function() {
if (_this.val().length === 0) {
_this.val(_this.attr("placeholder"))
}
})
})
}
}
})
})(jQuery);
推荐这篇文章看看: 这里
10 回答11.1k 阅读
6 回答3k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.6k 阅读✓ 已解决
3 回答5.1k 阅读✓ 已解决
5 回答1.9k 阅读
可以通过CSS来实现
Demo: http://codepen.io/rainyjune/p...