更改 HTML 表单“输入”通过 JavaScript 输入
我如何通过标签本身来做到这一点?
将类型从文本更改为密码
<input type='text' name='pass' />
是否可以在 input 标签本身将 type='text' 更改为 type='password'?
How can I do this through the tag itself?
Change type from text to password
<input type='text' name='pass' />
Is it possible to insert JavaScript code inside the input tag itself to change type='text' to type='password'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
尝试:
Try:
是的,您甚至可以通过触发事件来更改它
Yes, you can even change it by triggering an event
更改
的
type
会在某些浏览器(旧版 IE 和 Firefox 版本)中引发安全错误。您需要创建一个新的
input
元素,将其type
设置为您想要的,并从现有元素克隆所有其他属性。我在 jQuery 占位符插件中执行此操作: https: //github.com/mathiasbynens/jquery-placeholder/blob/master/jquery.placeholder.js#L80-84
要在 Internet Explorer 中工作:
下面的函数为您完成上述任务:
Changing the
type
of an<input type=password>
throws a security error in some browsers (old IE and Firefox versions).You’ll need to create a new
input
element, set itstype
to the one you want, and clone all other properties from the existing one.I do this in my jQuery placeholder plugin: https://github.com/mathiasbynens/jquery-placeholder/blob/master/jquery.placeholder.js#L80-84
To work in Internet Explorer:
The function below accomplishes the above tasks for you:
这是我的东西。
本质上,您正在使用 中的 onfocus 和 onblur 命令。标签来触发适当的 JavaScript 代码。它可能很简单:
此基本功能的改进版本会检查空字符串,并在出现空文本框时将密码输入返回到原始“密码”:
其中 HTML 如下所示:
Here is what I have for mine.
Essentially you are utilizing the onfocus and onblur commands in the <input> tag to trigger the appropriate JavaScript code. It could be as simple as:
An evolved version of this basic functionality checks for and empty string and returns the password input back to the original "Password" in the event of a null textbox:
Where HTML looks like:
我必须在 Evert 的末尾添加一个“.value”代码以使其正常工作。
此外,我还将其与浏览器检查结合起来,以便将 input type="number" 字段更改为 type="text" 在 Chrome 中,因为“formnovalidate”现在似乎不起作用。
I had to add a '.value' to the end of Evert's code to get it working.
Also I combined it with a browser check so that the input type="number" field is changed to type="text" in Chrome since 'formnovalidate' doesn't seem to work right now.
某些浏览器(如果我记得是 Internet Explorer)不支持此功能,但在其他浏览器中可以使用:
或
This is not supported by some browsers (Internet Explorer if I recall), but it works in the rest:
or
这是一个使用 jQuery 的简单切换。当您有 DataType 时,它也可以与 ASP.NET MVC EditorFor() 一起使用.模型属性的密码。
This is a simple toggle with jQuery. It works also with the the ASP.NET MVC EditorFor() when you have a DataType.Password on the model property.
你可以试试这个:
You can try this:
您可以使用 JavaScript setAttribute 方法
elementName.setAttribute('class', 'value');
You can use the JavaScript setAttribute method
elementName.setAttribute('class', 'value');