2011-10-29 29 views
0

PLZ看到下面的線程:
Set Default Value Of A Password
我followd答案#1這樣我的目的:設置默認值的密碼輸入/場景/股8

   <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' />"); 
    } 
} 

但我的代碼有問題,在IE 8和Firefox中,每件事情都可以/如何解決這個問題?

問題
點擊後再次是文本框從不注重....
我們是否有任何戰利品嗎?

回答

1

嘗試此代碼,看看它是否有幫助。我試圖模仿你的例子,所以它更有意義。

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'); 
    } 
}); 

另外一個js小提琴手看到一個工作示例http://jsfiddle.net/V2Dh5/3/

編輯

這是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); 
    } 
} 

看看js的小提琴手在http://jsfiddle.net/V2Dh5/17/

+0

感謝親愛的兄弟的接聽/但你的例子在IE 8有問題 - >不會在所有的工作!在火狐中,每件事都可以/我討厭IE 8的想法? – MoonLight

+0

爲以前的答案道歉@MoonLight你的權利,它不能在IE 8中工作。但我現在看到問題現在愚蠢的IE 8劑量允許你改變元素的類型屬性,當doument加載。因此,使其在IE 8中工作的唯一方法是替換整個輸入元素(請參閱編輯答案)。請記住代碼只是向您展示了這個概念。我不會推薦硬編碼完整的元素字符串,而是使用jquery元素appender。 – Bobby

+0

真的很感謝關注和編輯 - >如果我可以,我給你2票的答案/ setTimeout是一個好主意! – MoonLight