hw14_04_password.html 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <header>password</header>
  2. <body>
  3. <script>
  4. function Password(parent, open) {
  5. this.input = document.createElement("input");
  6. parent.append(this.input);
  7. //this.input.type = open ? "text" : "password";
  8. this.setValue = function (value) {
  9. this.input.value = value;
  10. }
  11. this.getValue = function () {
  12. return this.input.value;
  13. }
  14. this.setOpen = function (open) {
  15. this.input.type = open ? "text" : "password";
  16. }
  17. this.getOpen = function () {
  18. return this.input.type == "text";
  19. }
  20. this.input.onchange = function () {
  21. if (this.onChange)
  22. this.onChange(this.getValue());
  23. }.bind(this);
  24. let check = document.createElement("input");
  25. parent.append(check);
  26. check.onchange = function () {
  27. this.setOpen(check.checked);
  28. if (this.onOpenChange)
  29. this.onOpenChange(this.getOpen());
  30. }.bind(this);
  31. check.checked = open;
  32. check.value = "open";
  33. check.type = "checkbox";
  34. }
  35. let p = new Password(document.body, true);
  36. p.onChange = data => console.log(data)
  37. p.onOpenChange = open => console.log(open)
  38. p.setValue('qwerty')
  39. console.log(p.getValue())
  40. p.setOpen(false)
  41. console.log(p.getOpen())
  42. </script>
  43. </body>