经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
纯生js实现Element中input组件的部分功能(慢慢完善)并封装成组件
来源:cnblogs  作者:背着泰山找黄河  时间:2021/3/1 8:43:32  对本文有异议

现在实现的有基础用法、可清空、密码框,参考链接:https://element.eleme.cn/#/zh-CN/component/input

 

 

 

 HTML代码:想要测试哪个组件,直接将对应组件解开注释即可,标红的js和css记得修改成你自己的位置。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>js实现可清空input组件</title>
  6. <script src="../js/input/jsInput.js"></script>
  7. <link rel="stylesheet" type="text/css" href="../css/jsInput.css"/>
  8. </head>
  9. <body>
  10. <script>
  11. //普通input输入框
  12.  document.write(createElementInput())
  13. //添加可清空功能clearable
  14. //document.write(createElementInput("clearable"))
  15. //实现密码框show-password
  16. //document.write(createElementInput("show-password"))
  17. </script>
  18. </body>
  19. </html>

JS代码:

  1. function createElementInput(str){
  2. var temp = str;
  3. var html = "<div id='my_input_div' onmouseover='addClearNode(\""+str+"\")'' onmouseout='hiddenClearNode(\""+str+"\")''>";
  4. html += "<input id='my_input' placeholder='请输入内容'";
  5. if(str){
  6. if(str == 'show-password'){
  7. html+=" type = 'password' ";
  8. }
  9. }
  10. html += "oninput='addClearNode(\""+str+"\")'";
  11. html += "onclick='changeColor(\""+str+"\")'";
  12. html += "onblur='hiddenClearNode(\""+str+"\")'/>";
  13. if(str){
  14. html += "<input id='"+str+"' onmousedown='changeValue(\""+str+"\")'/>";
  15. }
  16. html += "</div>"
  17. return html;
  18. }
  19. //box-shadow: 0 0 0 20px pink; 通过添加阴影的方式显示边框
  20. function changeColor(str){
  21. //alert(str)
  22. document.getElementById("my_input_div").style.boxShadow="0 0 0 2px #409eff";
  23. //获取inpu的值
  24. var value = document.getElementById('my_input').value;
  25. var button = document.getElementById(str);
  26. //添加判断 如果输入框中有值 则显示清空按钮
  27. if(value){
  28. if(button){
  29. button.style.visibility = "visible"
  30. }
  31. }
  32. }
  33. //应该输入内容之后使用该事件
  34. function addClearNode(str){
  35. var value = document.getElementById('my_input').value;
  36. var button = document.getElementById(str);
  37. //alert(value)
  38. if(value){
  39. if(button){
  40. //将button设置为可见
  41. button.style.visibility = 'visible'
  42. }
  43. }else{
  44. //判断该属性是否存在
  45. if(button){
  46. //将button设置为不可见
  47. button.style.visibility = 'hidden'
  48. }
  49. }
  50. //选中后div添加选中样式 高亮显示
  51. document.getElementById("my_input_div").style.outline="0 0 0 2px #409eff";
  52. }
  53. //改变input中的值
  54. function changeValue(str){
  55. if(str){
  56. if(str == 'clearable'){
  57. clearValues(str);
  58. }else if(str == 'show-password'){
  59. showPassword();
  60. }
  61. }
  62. }
  63. //清空输入值
  64. function clearValues(str){
  65. document.getElementById("my_input").value = "";
  66. document.getElementById(str).style.visibility = "hidden";
  67. //仍然处于选中状态 div边框突出阴影
  68. document.getElementById("my_input_div").style.boxShadow="0 0 0 2px #409eff"
  69. }
  70. //隐藏清除按钮
  71. function hiddenClearNode(str){
  72. var button = document.getElementById(str);
  73. if(button){
  74. button.style.visibility="hidden";
  75. }
  76. //将div阴影设置为0
  77. document.getElementById("my_input_div").style.boxShadow="0 0 0"
  78. }
  79. //显示密码
  80. function showPassword(){
  81. var myInput = document.getElementById('my_input');
  82. var password = myInput.value;
  83. var type = myInput.type;
  84. //alert(type)
  85. if(type){
  86. if(type == 'password'){
  87. myInput.type = '';
  88. myInput.value = password;
  89. }else{
  90. myInput.type = 'password';
  91. myInput.value = password;
  92. }
  93. }
  94. //仍然处于选中状态 div边框突出阴影
  95. document.getElementById("my_input_div").style.boxShadow="0 0 0 2px #409eff"
  96. }

CSS代码:

  1. #my_input_div{
  2. width: 150px;
  3. border: 1px solid silver;
  4. border-radius: 4px;
  5. position: relative;
  6. }
  7. #my_input{
  8. height: 30px;
  9. width:100px;
  10. margin-left: 6px;
  11. border: none;
  12. outline: none;
  13. cursor: pointer;
  14. }
  15. #clearable{
  16. height: 20px;
  17. width: 15px;
  18. text-align: center;
  19. visibility:hidden;
  20. border: none;
  21. outline: none;
  22. color: #409eff;
  23. cursor: pointer;
  24. background-image: url(../image/clear.svg);
  25. background-repeat: no-repeat;
  26. background-size: 12px;
  27. position: absolute;
  28. top: 10px;
  29. left: 120px;
  30. display: inline-block;
  31. }
  32. #show-password{
  33. height: 20px;
  34. width: 15px;
  35. text-align: center;
  36. visibility:hidden;
  37. border: none;
  38. outline: none;
  39. color: #409eff;
  40. cursor: pointer;
  41. background-image: url(../image/eye.svg);
  42. background-repeat: no-repeat;
  43. background-size: 12px;
  44. position: absolute;
  45. top: 10px;
  46. left: 120px;
  47. display: inline-block;
  48. }

 

剩下的功能会慢慢被完善......

原文链接:http://www.cnblogs.com/qcq0703/p/14456418.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号