隐藏显示文本框内容
<style> input { color: #777777; } </style> </head> <body> <input type="text" value="手机" /> </body> <script> let ipt = document.querySelector("input"); ipt.onfocus = function () { if (this.value === "手机") { this.value = ""; } // 获得焦点需要把文本框里面的文字颜色变黑 this.style.color = "black"; }; ipt.onblur = function () { if (this.value === "") { this.value = "手机"; } // 失去焦点需要把文本框里面的文字颜色变浅色 this.style.color = "#777"; }; </script> <!-- onfocus 获取焦点 --> <!-- onblur失去焦点 -->

浙公网安备 33010602011771号