更改 HTML 表单“输入”通过 JavaScript 输入

发布于 2024-12-31 22:43:20 字数 301 浏览 1 评论 0原文

我如何通过标签本身来做到这一点?

将类型从文本更改为密码

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

毁虫ゝ 2025-01-07 22:43:20

尝试:

<input id="hybrid" type="text" name="password" />

<script type="text/javascript">
    document.getElementById('hybrid').type = 'password';
</script>

Try:

<input id="hybrid" type="text" name="password" />

<script type="text/javascript">
    document.getElementById('hybrid').type = 'password';
</script>
紫轩蝶泪 2025-01-07 22:43:20

是的,您甚至可以通过触发事件来更改它

<input type='text' name='pass'  onclick="(this.type='password')" />


<input type="text" placeholder="date" onfocusin="(this.type='date')" onfocusout="(this.type='text')">

Yes, you can even change it by triggering an event

<input type='text' name='pass'  onclick="(this.type='password')" />


<input type="text" placeholder="date" onfocusin="(this.type='date')" onfocusout="(this.type='text')">
Oo萌小芽oO 2025-01-07 22:43:20

更改 type 会在某些浏览器(旧版 IE 和 Firefox 版本)中引发安全错误。

您需要创建一个新的 input 元素,将其 type 设置为您想要的,并从现有元素克隆所有其他属性。

我在 jQuery 占位符插件中执行此操作: https: //github.com/mathiasbynens/jquery-placeholder/blob/master/jquery.placeholder.js#L80-84

要在 Internet Explorer 中工作:

  • 动态创建一个新元素
  • ,复制属性将旧元素转换为新元素
  • 将新元素的类型设置为新类型
  • 用新元素替换旧元素

下面的函数为您完成上述任务:

<script>
function changeInputType(oldObject, oType) {
    var newObject = document.createElement('input');
    newObject.type = oType;
    if(oldObject.size) newObject.size = oldObject.size;
    if(oldObject.value) newObject.value = oldObject.value;
    if(oldObject.name) newObject.name = oldObject.name;
    if(oldObject.id) newObject.id = oldObject.id;
    if(oldObject.className) newObject.className = oldObject.className;
    oldObject.parentNode.replaceChild(newObject,oldObject);
    return newObject;
}
</script>

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 its type 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:

  • dynamically create a new element
  • copy the properties of the old element into the new element
  • set the type of the new element to the new type
  • replace the old element with the new element

The function below accomplishes the above tasks for you:

<script>
function changeInputType(oldObject, oType) {
    var newObject = document.createElement('input');
    newObject.type = oType;
    if(oldObject.size) newObject.size = oldObject.size;
    if(oldObject.value) newObject.value = oldObject.value;
    if(oldObject.name) newObject.name = oldObject.name;
    if(oldObject.id) newObject.id = oldObject.id;
    if(oldObject.className) newObject.className = oldObject.className;
    oldObject.parentNode.replaceChild(newObject,oldObject);
    return newObject;
}
</script>
南汐寒笙箫 2025-01-07 22:43:20

这是我的东西。

本质上,您正在使用 中的 onfocusonblur 命令。标签来触发适当的 JavaScript 代码。它可能很简单:

<span><input name="login_text_password" type="text" value="Password" onfocus="this.select(); this.setAttribute('type','password');" onblur="this.select(); this.setAttribute('type','text');" /></span>

此基本功能的改进版本会检查空字符串,并在出现空文本框时将密码输入返回到原始“密码”:

<script type="text/javascript">
    function password_set_attribute() {
        if (document.getElementsByName("login_text_password")[0].value.replace(/\s+/g, ' ') == "" ||
            document.getElementsByName[0].value == null) {

            document.getElementsByName("login_text_password")[0].setAttribute('type','text')
            document.getElementsByName("login_text_password")[0].value = 'Password';
        }
        else {
            document.getElementsByName("login_text_password")[0].setAttribute('type','password')
        }
    }
</script>

其中 HTML 如下所示:

<span><input name="login_text_password" class="roundCorners" type="text" value="Password" onfocus="this.select(); this.setAttribute('type','password');" onblur="password_set_attribute();" /></span>

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:

<span><input name="login_text_password" type="text" value="Password" onfocus="this.select(); this.setAttribute('type','password');" onblur="this.select(); this.setAttribute('type','text');" /></span>

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:

<script type="text/javascript">
    function password_set_attribute() {
        if (document.getElementsByName("login_text_password")[0].value.replace(/\s+/g, ' ') == "" ||
            document.getElementsByName[0].value == null) {

            document.getElementsByName("login_text_password")[0].setAttribute('type','text')
            document.getElementsByName("login_text_password")[0].value = 'Password';
        }
        else {
            document.getElementsByName("login_text_password")[0].setAttribute('type','password')
        }
    }
</script>

Where HTML looks like:

<span><input name="login_text_password" class="roundCorners" type="text" value="Password" onfocus="this.select(); this.setAttribute('type','password');" onblur="password_set_attribute();" /></span>
我还不会笑 2025-01-07 22:43:20

我必须在 Evert 的末尾添加一个“.value”代码以使其正常工作。

此外,我还将其与浏览器检查结合起来,以便将 input type="number" 字段更改为 type="text" 在 Chrome 中,因为“formnovalidate”现在似乎不起作用。

if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
    document.getElementById("input_id").attributes["type"].value = "text";

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.

if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
    document.getElementById("input_id").attributes["type"].value = "text";
你怎么这么可爱啊 2025-01-07 22:43:20
$(".show-pass").click(function (e) {
    e.preventDefault();
    var type = $("#signupform-password").attr('type');
    switch (type) {
        case 'password':
        {
            $("#signupform-password").attr('type', 'text');
            return;
        }
        case 'text':
        {
            $("#signupform-password").attr('type', 'password');
            return;
        }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="password" class="show-pass">

$(".show-pass").click(function (e) {
    e.preventDefault();
    var type = $("#signupform-password").attr('type');
    switch (type) {
        case 'password':
        {
            $("#signupform-password").attr('type', 'text');
            return;
        }
        case 'text':
        {
            $("#signupform-password").attr('type', 'password');
            return;
        }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="password" class="show-pass">

轻许诺言 2025-01-07 22:43:20
let btn = document.querySelector('#btn');
let input = document.querySelector('#username');

btn.addEventListener('click',()=> {
    if ( input.type === "password") {
        input.type = "text"
    } else {
        input.type = "password"


    }
})
<input type="password" id="username" >

<button id="btn">change Attr</button>

let btn = document.querySelector('#btn');
let input = document.querySelector('#username');

btn.addEventListener('click',()=> {
    if ( input.type === "password") {
        input.type = "text"
    } else {
        input.type = "password"


    }
})
<input type="password" id="username" >

<button id="btn">change Attr</button>

御弟哥哥 2025-01-07 22:43:20

某些浏览器(如果我记得是 Internet Explorer)不支持此功能,但在其他浏览器中可以使用:

document.getElementById("password-field").attributes["type"] = "password";

document.getElementById("password-field").attributes["type"] = "text";

This is not supported by some browsers (Internet Explorer if I recall), but it works in the rest:

document.getElementById("password-field").attributes["type"] = "password";

or

document.getElementById("password-field").attributes["type"] = "text";
江挽川 2025-01-07 22:43:20

这是一个使用 jQuery 的简单切换。当您有 DataType 时,它​​也可以与 ASP.NET MVC EditorFor() 一起使用.模型属性的密码。

    function showPassword() {
        let password = $(".password");

        if (password[0].type == "password") {
            password[0].type = "";
        }
        else {
            password[0].type = "password";
        }
    }

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.

    function showPassword() {
        let password = $(".password");

        if (password[0].type == "password") {
            password[0].type = "";
        }
        else {
            password[0].type = "password";
        }
    }
幻梦 2025-01-07 22:43:20

你可以试试这个:

const myTimeout = setTimeout(show, 5000);

function show() {
  document.getElementById('pass').type = "text";
}

clearTimeout(myTimeout);

You can try this:

const myTimeout = setTimeout(show, 5000);

function show() {
  document.getElementById('pass').type = "text";
}

clearTimeout(myTimeout);
自此以后,行同陌路 2025-01-07 22:43:20
 //html
 <input type="password" id="password_input">
 <i onclick="passwordDisplay()" class="ti-eye"></i>

 //js
 const input = document.getElementById("password_input")

 function passwordDisplay() { 
    if (input.attributes["type"].value == "text")
        input.attributes["type"].value = "password"
     else
        input.attributes["type"].value = "text"
    
 }
 //html
 <input type="password" id="password_input">
 <i onclick="passwordDisplay()" class="ti-eye"></i>

 //js
 const input = document.getElementById("password_input")

 function passwordDisplay() { 
    if (input.attributes["type"].value == "text")
        input.attributes["type"].value = "password"
     else
        input.attributes["type"].value = "text"
    
 }
凉栀 2025-01-07 22:43:20

您可以使用 JavaScript setAttribute 方法
elementName.setAttribute('class', 'value');

You can use the JavaScript setAttribute method
elementName.setAttribute('class', 'value');

空宴 2025-01-07 22:43:20
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript" language="javascript">
function changefield(){
    document.getElementById("passwordbox").innerHTML = "<input id=\"passwordfield\" type=\"password\" name=\"password-field\" title=\"Password\" tabindex=\"2\" />";
    document.getElementById("password-field".focus();
}
</script>
</head>

<body>
<div id="passwordbox">
<input id="password-field" type="text" name="password-field" title="Password"onfocus="changefield();" value="Password" tabindex="2" />
</div>
<input type="submit" name="submit" value="sign in" tabindex="3" />

</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript" language="javascript">
function changefield(){
    document.getElementById("passwordbox").innerHTML = "<input id=\"passwordfield\" type=\"password\" name=\"password-field\" title=\"Password\" tabindex=\"2\" />";
    document.getElementById("password-field".focus();
}
</script>
</head>

<body>
<div id="passwordbox">
<input id="password-field" type="text" name="password-field" title="Password"onfocus="changefield();" value="Password" tabindex="2" />
</div>
<input type="submit" name="submit" value="sign in" tabindex="3" />

</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文