jQuery制作input提示内容(兼容IE8以上)
2019/6/27 20:50:29
本文主要是介绍jQuery制作input提示内容(兼容IE8以上),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
我们都知道HTML5的input新属性有 placeholder="",那么这个不兼容IE低版本我们只能用脚本来写了。
首先HTML新建一个input
<input type="text" class="input" value="请输入搜索内容" />
然后我们再引入相应的js库,再使用jQuery
<script src="js/jquery-1.8.3.min.js"></script> <script> $(".input").bind({ focus:function(){ if (this.value == this.defaultValue){ this.value=""; } }, blur:function(){ if (this.value == ""){ this.value = this.defaultValue; } } }); </script>
简单吧,这样就可以了,那么这个是input的属性是text,我们要密码password也一样可以显示IE低版本,我们用的方法就是用两个input,一个text默认显示,一个password默认隐藏,当text获取焦点时password显示,text隐藏,没有输入内容失去焦点text显示,password显示,好了,废话不多说,看代码!
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>密码框提示</title> </head> <body> <input type="text" value="登录密码" class="show" > <input type="password" class="log_paw" style="display: none;"> <script src="js/jquery-1.8.3.min.js"></script> <script> $('.show').focus(function(){ var text_value = $(this).val(); if (text_value == this.defaultValue) { $(this).hide(); $(this).next('input.log_paw').show().focus(); } }); $('input.log_paw').blur(function() { var text_value = $(this).val(); if (text_value == '') { $(this).prev('.show').show(); $(this).hide(); } }); </script> </body> </html>
好了,代码就在这里了,希望能帮助到你!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
这篇关于jQuery制作input提示内容(兼容IE8以上)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-30React Native常用组件-点击组件
- 2024-05-30uniapp+vue3+uv-ui手机端后台OA管理模板
- 2024-05-29Python网络爬虫的时候json=就是让你少写个json.dumps()
- 2024-05-27React Native常用组件-展示组件
- 2024-05-27React Native常用组件-列表组件
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist