问题
plz see the below thread :
Set Default Value Of A Password
i followd the answer #1 like this for my purpose :
<div id="headertxtPassWord-Container" class="header-login-input-Container">
<asp:TextBox ID="headertxtPassWord" runat="server" Text="password"
onclick="return onclickOfPassWord(this);" onblur="onblurOfPassWord(this);"
CssClass="header-login-input"></asp:TextBox>
</div>
function onclickOfPassWord(This) {
if (This.value == 'password') {
$('#headertxtPassWord-Container').html("<input id='headertxtPassWord' name='headertxtPassWord' type='password' value='' onclick='return onclickOfPassWord(this);' onblur='onblurOfPassWord(this);' class='header-login-input' />");
$('#headertxtPassWord').focus();
}
}
function onblurOfPassWord(This) {
if (This.value == '') {
$('#headertxtPassWord-Container').html("<input id='headertxtPassWord' name='headertxtPassWord' value='password' onclick='return onclickOfPassWord(this);' onblur='onblurOfPassWord(this);' class='header-login-input' />");
}
}
but my codes has a problem in IE 8 and in firefox every thing is ok / how can fix that ?
problem
that textbox never focus again after click ....
do we have any loot here ?
回答1:
Try this code and see if it helps. I tried to mimic your example so it makes more sense.
HTML
<div id="headertxtPassWord-Container" class="header-login-input-Container">
<input id="headertxtPassWord" value="password" class="header-login-input" />
</div>
JQuery
$('.header-login-input').bind('click', function() {
if ($(this).val() === "password")
{
this.type = "password";
$(this).val('');
}
});
$('.header-login-input').bind('blur', function() {
if ($(this).val() === "")
{
this.type = "text";
$(this).val('password');
}
});
Also a js fiddler to see a working example http://jsfiddle.net/V2Dh5/3/
Edited
This is the fix for IE 8
$('.header-login-input').live('click', PopulateElement);
$('.header-login-input').live('blur', function() {
if ($(this).val() === "")
{
$(".header-login-input-Container").html('');
$(".header-login-input-Container").html("<input id=\"headertxtPassWord\" name=\"headertxtPassWord\" class=\"header-login-input\" value=\"password\" type=\"text\"/>");
}
});
function PopulateElement () {
if ($(this).val() === "password")
{
$(".header-login-input-Container").html('');
$(".header-login-input-Container").html("<input id=\"headertxtPassWord\" name=\"headertxtPassWord\" class=\"header-login-input\" type=\"password\"/>");
setTimeout(function() { $("#headertxtPassWord").focus()}, 10);
}
}
Look at the js fiddler at http://jsfiddle.net/V2Dh5/17/
来源:https://stackoverflow.com/questions/7938885/set-default-value-of-a-password-input-a-scenario-issue-in-ie-8