光标焦点不在密码字段中。

时间:2011-07-30 09:21:36

标签: javascript html onfocus

我使用以下代码将输入文本字段交换到焦点上的密码区域。

<input type="text" name="password" id="password" onblur="ChangeToTextField(); if (this.value == '') {this.value = 'password';}" onfocus="ChangeToPassField(); if (this.value == 'password') { this.value = '';}else { this.value; }" value="password" style="width:230px; height:25px; margin-left:5px; background:none; border:0px;" />


<script type="text/javascript">
    function ChangeToPassField() {
    document.getElementById('password').type="password";
    var input = document.getElementById('password');
    input.focus();
  }
  function ChangeToTextField() {
   if(document.getElementById('password').value=="" || document.getElementById('password').value == "password" )
       {
   //alert("hai");
   document.getElementById('password').type="text";
    document.getElementById('password').value="password";
       }
  }
</script>

如果我在密码字段中单击,则光标不会在密码字段中闪烁。如何在密码字段中显示光标指针?...

2 个答案:

答案 0 :(得分:0)

使用HTML 5 placeholder属性怎么样? (请记住,它无处不在)

<!DOCTYPE html>
<!-- ... -->
<form>
    <input placeholder="Password" type="password" name="password" id="password" />
    <input type="submit" />
</form>

jsfiddle:http://jsfiddle.net/ndduP/


否则,这是尝试你要做的事情(请记住,从安全/隐私的角度来看,有些人可能会关闭javascript使其成为问题)

<form>
    <input type="text" name="password" id="password_field" value="Password" onfocus="make_pass();" onblur="make_text();" />
    <input type="submit" />
</form>

<script>
function make_text() {
    var field = document.getElementById('password_field');
    if (field.value.replace(/ /g,'') === "") {
        field.type  = "text";
        field.value = "Password";
    }
}

function make_pass() {
    var field = document.getElementById('password_field');
    if (field.type === "text") {
        field.value = "";
        field.type  = "password";
    }
}
</script>

jsfiddle:http://jsfiddle.net/utEVx/

答案 1 :(得分:0)

<input type="text" name="password" id="password" value="password" style="width:230px; height:25px; margin-left:5px; background:none; border:0px;" />

<script>
var passwordF = document.getElementById("password");

passwordF.onblur = function() {
   ChangeToTextField();
   if (this.value === '') {
      this.value = '';
   }
};

passwordF.onfocus = function() {
   ChangeToPassField();
   if (this.value === 'password') {
      this.value = '';
   }
};

function ChangeToPassField() {
   console.log("changed to pass field...");
   document.getElementById('password').type="password";
   var input = document.getElementById('password');
   input.focus();
}

function ChangeToTextField() {
   console.log("changed to text field...");
   if(document.getElementById('password').value=="" ||    
      document.getElementById('password').value == "password" )
   {
      //alert("hai");
      document.getElementById('password').type="text";
      document.getElementById('password').value="password";
   }
} 
</script>