js实现Element中input组件的部分功能并封装成组件(实例代码)

简介: 这篇文章主要介绍了纯生js实现Element中input组件的部分功能(慢慢完善)并封装成组件,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

这篇文章主要介绍了纯生js实现Element中input组件的部分功能(慢慢完善)并封装成组件,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下


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

网络异常,图片无法展示
|

网络异常,图片无法展示
|

网络异常,图片无法展示
|

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<!DOCTYPE html>

<html>

  <head>

    <metacharset="utf-8">

    <title>js实现可清空input组件</title>

    <scriptsrc="../js/input/jsInput.js"></script>

    <linkrel="stylesheet"type="text/css"href="../css/jsInput.css"/>

  </head>

  <body>

    <script>

      //普通input输入框

       document.write(createElementInput())

      //添加可清空功能clearable

      //document.write(createElementInput("clearable"))

      //实现密码框show-password

      //document.write(createElementInput("show-password"))

    </script>

  </body>

</html>

JS代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

functioncreateElementInput(str){

  vartemp = str;

  varhtml = "<div id='my_input_div' onmouseover='addClearNode(\""+str+"\")'' onmouseout='hiddenClearNode(\""+str+"\")''>";

  html += "<input id='my_input' placeholder='请输入内容'";

  if(str){

     if(str == 'show-password'){

       html+=" type = 'password' ";

     }

  }

  html += "oninput='addClearNode(\""+str+"\")'";

  html += "onclick='changeColor(\""+str+"\")'";

  html += "onblur='hiddenClearNode(\""+str+"\")'/>";

  if(str){

   html += "<input id='"+str+"' onmousedown='changeValue(\""+str+"\")'/>";

  

  html += "</div>"

  returnhtml;

}

 

//box-shadow: 0 0 0 20px pink; 通过添加阴影的方式显示边框

functionchangeColor(str){

  //alert(str)

  document.getElementById("my_input_div").style.boxShadow="0 0 0 2px #409eff";

  //获取inpu的值

  varvalue = document.getElementById('my_input').value;

  varbutton = document.getElementById(str);

  //添加判断 如果输入框中有值 则显示清空按钮

  if(value){

    if(button){

      button.style.visibility = "visible"

    }

  }

}

//应该输入内容之后使用该事件

functionaddClearNode(str){

  varvalue = document.getElementById('my_input').value;

  varbutton = document.getElementById(str);

  //alert(value)

  if(value){

    if(button){

      //将button设置为可见

      button.style.visibility = 'visible'

    }

  }else{

    //判断该属性是否存在

    if(button){

      //将button设置为不可见

      button.style.visibility = 'hidden'

    }

  }

  //选中后div添加选中样式 高亮显示

  document.getElementById("my_input_div").style.outline="0 0 0 2px #409eff";

}

//改变input中的值

functionchangeValue(str){

  if(str){

    if(str == 'clearable'){

      clearValues(str);

    }elseif(str == 'show-password'){

      showPassword();

    }

  }

  

}

//清空输入值

functionclearValues(str){

  document.getElementById("my_input").value = "";

  document.getElementById(str).style.visibility = "hidden";

  //仍然处于选中状态 div边框突出阴影

  document.getElementById("my_input_div").style.boxShadow="0 0 0 2px #409eff"

}

 

//隐藏清除按钮

functionhiddenClearNode(str){

  varbutton = document.getElementById(str);

  if(button){

    button.style.visibility="hidden";

  }

  //将div阴影设置为0

  document.getElementById("my_input_div").style.boxShadow="0 0 0"

}

 

//显示密码

functionshowPassword(){

  varmyInput = document.getElementById('my_input');

  varpassword = myInput.value;

  vartype = myInput.type;

  //alert(type)

  if(type){

    if(type == 'password'){

      myInput.type = '';

      myInput.value = password;

    }else{

      myInput.type = 'password';

      myInput.value = password;

    }

  }

  //仍然处于选中状态 div边框突出阴影

  document.getElementById("my_input_div").style.boxShadow="0 0 0 2px #409eff"

}

CSS代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

#my_input_div{

  width150px;

  border1pxsolidsilver;

  border-radius: 4px;

  positionrelative;

}

#my_input{

  height30px;

  width:100px;

  margin-left6px;

  bordernone;

  outlinenone;

  cursorpointer;

}

#clearable{

  height20px;

  width15px;

  text-aligncenter;

  visibility:hidden;

  bordernone;

  outlinenone;

  color#409eff;

  cursorpointer;

  background-imageurl(../image/clear.svg);

  background-repeatno-repeat;

  background-size12px;

  positionabsolute;

  top10px;

  left120px;

  display: inline-block;

}

#show-password{

  height20px;

  width15px;

  text-aligncenter;

  visibility:hidden;

  bordernone;

  outlinenone;

  color#409eff;

  cursorpointer;

  background-imageurl(../image/eye.svg);

  background-repeatno-repeat;

  background-size12px;

  positionabsolute;

  top10px;

  left120px;

  display: inline-block;

}

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

到此这篇关于纯生js实现Element中input组件的部分功能(慢慢完善)并封装成组件的文章就介绍到这了,更多相关js实现input组件功能内容请搜索米米素材网以前的文章或继续浏览下面的相关文章希望大家以后多多支持米米素材网!

原文链接:https://www.mimisucai.com/teach/javascript/36044.html

相关文章
|
7天前
|
JavaScript 前端开发
js实现点击音频实现播放功能
js实现点击音频实现播放功能
|
7天前
|
前端开发 JavaScript
使用JavaScript实现复杂功能:构建一个自定义的拖拽功能
使用JavaScript实现复杂功能:构建一个自定义的拖拽功能
|
10天前
|
JSON JavaScript 前端开发
JavaScript原生代码处理JSON的一些高频次方法合集
JavaScript原生代码处理JSON的一些高频次方法合集
|
10天前
sd.js 2.0封装:更加简化请求传参内容(逐步废弃、逐渐日落2024.01.02)
sd.js 2.0封装:更加简化请求传参内容(逐步废弃、逐渐日落2024.01.02)
|
30天前
|
存储 JavaScript 前端开发
非常实用的JavaScript一行代码(整理总结)
非常实用的JavaScript一行代码(整理总结)
27 0
|
1月前
|
JavaScript 前端开发 测试技术
如何编写JavaScript模块化代码
如何编写JavaScript模块化代码
12 0
|
6天前
|
JavaScript 前端开发
【掰开揉碎】JavaScript状态机的应用场景与实例(二)
【掰开揉碎】JavaScript状态机的应用场景与实例(二)
|
6天前
|
前端开发 JavaScript 测试技术
【掰开揉碎】JavaScript状态机的应用场景与实例(一)
【掰开揉碎】JavaScript状态机的应用场景与实例(一)
|
7天前
|
JavaScript
|
7天前
|
存储 前端开发 JavaScript
使用JavaScript实现复杂功能——一个交互式音乐播放器
使用JavaScript实现复杂功能——一个交互式音乐播放器