如何删除并显示单词“密码”在密码字段中
我使用下面的代码作为密码字段。我如何增强它,显示“密码”一词,然后单击该字段,输入的任何内容都应以密码格式完成,其中条目用点掩盖。
我正在使用 PHP。
代码:
<input type="password" name="password" maxlength="50" id="pass"
onclick="this.value='';" onfocus="this.select()"
onblur="this.value=!this.value?'Password':this.value;" value="Password">
I am using the code below for the password field. How can i enhance it so show the word Password and then once the field is clicked and whatever is being entered should be done in the password format where the entries are masked with the dots..
I am using PHP.
Code:
<input type="password" name="password" maxlength="50" id="pass"
onclick="this.value='';" onfocus="this.select()"
onblur="this.value=!this.value?'Password':this.value;" value="Password">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试使用 jQuery
try using jQuery
对于支持 HTML5 的浏览器,您可以简单地使用占位符属性,a la:
这将向支持 HTML5 的用户显示“密码此处”,但当用户开始输入时,它将显示星号。上面的其他答案已经显示了一些与 Javascript 相关的答案,但很快我们甚至不需要这些:)
For HTML5-enabled browsers, you can simply use the placeholder attribute, a la:
This will show "Password Here" to HTML5-enabled users, but when the user starts typing, it will show the asterisks instead. Other answers above have shown some Javascript-related answers, but soon we won't even need those :)
这不是真的,使用 JavaScript 是可能的。这是有关如何操作的教程。本教程使用 jQuery,但通过编写您自己的 JS 脚本也可以实现相同的结果。
密码输入的 jQuery 切换
干杯
That is not true, it is possible using JavaScript. Here is a tutorial on how to do it. This tut uses jQuery but the same results could be achieved by writing your own JS script.
jQuery toggle of password input
cheers
通过各种 JavaScript 选项,这绝对是可能的。为了补充 a.stgeorge 发布的有关使用 jquery 的内容,您还可以编写自己的 JS 来执行此操作。
在此处查看实际操作:http://www.moditekka.com/pwsample.html
HTML 示例:
JavaScript 片段(这在 IE 中失败,见下文):
编辑:
如果有人还在读这篇文章,我刚刚意识到我的简洁代码在 IE 中不起作用,这要归功于西雅图的一个明智决定,即只有在将元素添加到 DOM 后才读取“type”属性。为了适应这一点,我更新了下面的 JavaScript 代码:
This is absolutely possible through a variety of JavaScript options. To supplement what a.stgeorge posted about using jquery, you can also write your own JS to do it.
See it in action here: http://www.moditekka.com/pwsample.html
HTML sample:
JavaScript snippet (this fails in IE, see below):
EDIT:
If anyone is still reading this I just realized that my neat code won't work in IE, thanks to a clever decision in Seattle to make the "type" property read only once an element has been added to the DOM. To accommodate this I've updated my JavaScript code below:
由于安全限制,这是不可能的。
您可以使用以下代码片段尝试一下(但它不会起作用)
This is not possible due to security restrictions.
You can try it (but it won't work) using the following snippet
你也可以像 tumblr 那样做。基本上他们有一个div来包裹输入框。该 div 包括表单标签和输入。为简单起见,忽略图像更改。但我认为你必须处理透明图像/不透明度才能使其按照他们的方式工作。由于标签位于表单输入框的后面。
如果您在单击框中时观看 Firebug,它会在 div 周围添加第二类。 (此时这与图像有更多关系。)但是当您开始输入时,会包含第三个类,该类将标签设置为显示:none;
因此,以更简单的方式回顾一下:
Div 包装了标签和输入框。 div 是相对定位的。标签绝对位于输入框后面。
使用 onclick 他们添加半透明状态/交换背景图像。当您开始输入时,它会将标签设置为不显示;
You can also do like what tumblr does. Basically they have a div that wraps the input box. This div includes both the form label and the input. Ignore the image change for simplicity. But I think you will have to deal with transparent images / opacity to get it to work how they do it. Since the label is behind the form input box.
If you watch firebug as you click in the box it adds a 2nd class around the div. (this has more to do with the image at this point.) But when you start typing a 3rd class gets included that sets the label to display: none;
So to recap in a simpler way:
Div wraps a label and input box. The div is relative positioned. The label is absolutely positioned behind the input box.
Using onclick they add a semi transparent state / swap the background image. When you start typing it sets the label to display none;
用纯JS
With pure JS