HTML5 创建隐藏文本的字段,但可以复制/粘贴

Tim*_* H. 1 html password-protection

我想在我的 HTML5 应用程序中添加一个字段,我可以在其中写入密码,并且在保存时,密码将被 **** 屏蔽。这样当与客户一起访问页面时,他看不到密码,但我需要能够复制它。所以复制它的“显示密码”按钮不起作用。

有没有办法做到这一点?我很遗憾没有找到任何东西。

小智 5

您可以做的是在按钮上创建一个 onclick 功能,然后

<!-- The text field -->
<!- The value passed is for just testing user will enter the password -->
<input type="password" value="Hello World" id="myInput">

<!-- The button used to copy the text -->
<button onclick="myFunction()">Copy text</button>
Run Code Online (Sandbox Code Playgroud)

MyFunction() 然后将

function myFunction() {
  /* Get the text field */
  var copyText = document.getElementById("myInput");

  /* Select the text field */
  copyText.select();
  copyText.setSelectionRange(0, 99999); /*For mobile devices*/

  /* Copy the text inside the text field */
  document.execCommand("copy");

  /* Alert the copied text */
  alert("Copied the text: " + copyText.value);
}
Run Code Online (Sandbox Code Playgroud)

参考:https : //www.w3schools.com/howto/howto_js_copy_clipboard.asp

我希望它有帮助。