javascript - 将输入类型 ="text"更改为输入类型 ="password"onfocus()

标签 javascript html asp.net forms textbox

到目前为止,我已经完成了第 1 部分,我使用此 javascript 成功地将输入类型从文本更改为密码:

function replaceT(obj) {
        var newO = document.createElement('input');
        newO.setAttribute('type', 'password');
        newO.setAttribute('class', 'textbox');
        newO.setAttribute('name', obj.getAttribute('name'));
        obj.parentNode.replaceChild(newO, obj);
        newO.focus();
    } 

和ASP代码:

<asp:TextBox ID="txtPassword" runat="server" SkinID="txtLogin" value="Password..." TabIndex="2" onfocus="replaceT(this)"></asp:TextBox>

我要的是第二部分。如何在 blur 上返回“密码...”值。

简而言之,我想实现一个类似 facebook 的密码文本框。在 IE 上起作用的占位符属性。

这是 focus() 之前的情况:

enter image description here

然后在焦点之后:

enter image description here

然后返回这个onblur():

谢谢。

附言

我想在没有 jQuery(纯 javascript)的情况下实现它,并且我正在使用 IE 7+。

最佳答案

你不需要替换组件,只需将thistype设置为password,如下代码:

<asp:TextBox ID="txtPassword" runat="server" SkinID="txtLogin" value="Password..." TabIndex="2" onfocus="this.type='password'">

这是一个 jsfiddle 工作示例:http://jsfiddle.net/mZyTG/

更新

我没有测试这段代码,但它向 newO 添加了一个 onblur 事件监听器,并且在调用时将输入替换为旧的。

function replaceT(obj) {
        var newO = document.createElement('input');
        newO.setAttribute('type', 'password');
        newO.setAttribute('class', 'textbox');
        newO.setAttribute('name', obj.getAttribute('name'));
        newO.addEventListener("onblur", function(){newO.parentNode.replaceChild(obj, newO)}, false);
        obj.parentNode.replaceChild(newO, obj);
        newO.focus();
    } 

关于javascript - 将输入类型 ="text"更改为输入类型 ="password"onfocus(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17398409/

相关文章:

c# - :disabled attribute on my button does not seem to work

c# - 带有 HTML5 控件的 Visual Studios Formview

javascript - 我如何有选择地在 Typescript 接口(interface)中嵌入一个或多个接口(interface)?

javascript - 使用路由添加的 Controller 在 Angular JS 中声明 $scope 变量?

javascript - 下拉菜单中的 OnClick() 事件

javascript - 我是否将 Javascript 用于 HTML <select> 需要影响两个变量?

Javascript帮助,可以使用jQuery

javascript - 如何使用 jquery/javascript、Spring 按 id 删除?

HTML5 历史 API

c# - asp.net 在更改下拉列表时使用 javascript 填充下拉列表