扫码关注官方订阅号
获取焦点时移除placeholder,失去焦点重新获得placeholder内容
光阴似箭催人老,日月如移越少年。
可以通过CSS来实现
Demo: http://codepen.io/rainyjune/p...
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);
推荐这篇文章看看: 这里
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
可以通过CSS来实现
Demo: http://codepen.io/rainyjune/p...
原生JS...
我很早很早之前写了一个方法:
推荐这篇文章看看: 这里