当前位置:Gxlcms > JavaScript > jQuery制作input提示内容(兼容IE8以上)

jQuery制作input提示内容(兼容IE8以上)

时间:2021-07-01 10:21:17 帮助过:61人阅读

我们都知道HTML5的input新属性有 placeholder="",那么这个不兼容IE低版本我们只能用脚本来写了。

首先HTML新建一个input

  1. <input type="text" class="input" value="请输入搜索内容" />

然后我们再引入相应的js库,再使用jQuery

  1. <script src="js/jquery-1.8.3.min.js"></script>
  2. <script>
  3. $(".input").bind({
  4. focus:function(){
  5. if (this.value == this.defaultValue){
  6. this.value="";
  7. }
  8. },
  9. blur:function(){
  10. if (this.value == ""){
  11. this.value = this.defaultValue;
  12. }
  13. }
  14. });
  15. </script>

简单吧,这样就可以了,那么这个是input的属性是text,我们要密码password也一样可以显示IE低版本,我们用的方法就是用两个input,一个text默认显示,一个password默认隐藏,当text获取焦点时password显示,text隐藏,没有输入内容失去焦点text显示,password显示,好了,废话不多说,看代码!

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>密码框提示</title>
  6. </head>
  7. <body>
  8. <input type="text" value="登录密码" class="show" >
  9. <input type="password" class="log_paw" style="display: none;">
  10. <script src="js/jquery-1.8.3.min.js"></script>
  11. <script>
  12. $('.show').focus(function(){
  13. var text_value = $(this).val();
  14. if (text_value == this.defaultValue) {
  15. $(this).hide();
  16. $(this).next('input.log_paw').show().focus();
  17. }
  18. });
  19. $('input.log_paw').blur(function() {
  20. var text_value = $(this).val();
  21. if (text_value == '') {
  22. $(this).prev('.show').show();
  23. $(this).hide();
  24. }
  25. });
  26. </script>
  27. </body>
  28. </html>

好了,代码就在这里了,希望能帮助到你!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

人气教程排行