2012-02-11 9 views
0

のラベルを隠す:jQueryの文句を言わない私が<a href="http://jsfiddle.net/buyC9/148/" rel="nofollow">here(JSFiddle)</a>ライブのコードを持っているキー操作

jQueryのキー操作のラベルを非表示にしていません。

のjQuery:

$(document).ready(function(){ 
$('input').keypress(function(){ 
$(this).closest('label').hide(); 
}); 
}); 

はHTML:

<div id="login"> 
<h2 style="font-size:18px;">Login</h2> 

<form method="post" id="user_new" class="user_new" action="https://stackoverflow.com/users/sign_in" accept-charset="UTF-8"><div style="margin:0;padding:0;display:inline"><input type="hidden" value="✓" name="utf8"><input type="hidden" value="CFIZzyFucEG03h33m6dzX9DNy/Ux11yjShcT2xZnZRs=" name="authenticity_token"></div> 
    <p style="position: relative; float: left; margin-bottom: 0px;"><label style="position:absolute;margin:0px;top:10px;left:20px;color:#CCCCCC;" for="user_email">Email</label> 
    <input type="email" value="" size="30" name="user[email]" id="user_email"> 
    </p> 

    <p style="position: relative; float: left; margin-top: 5px;"><label style="position:absolute;margin:0px;top:10px;left:20px;color:#CCCCCC;" for="user_kodeord">Kodeord</label> 
    <input type="password" size="30" name="user[password]" id="user_password"> 
    </p> 

    <p><input type="hidden" value="0" name="user[remember_me]"><input type="checkbox" value="1" name="user[remember_me]" id="user_remember_me"> <label for="user_Husk_mig">Husk mig</label></p> 
    <p><input type="submit" value="Login" name="commit" id="user_submit"></p> 
<div style=" display: block; 
    float: left; 
    margin-top: 20px; 
    width: 400px;margin-left: -20px;"> 
    <a href="https://stackoverflow.com/users/sign_up">Sign up</a><br> 

    <a href="https://stackoverflow.com/users/password/new">Forgot your password?</a><br> 



</div> 
</form></div> 

答えて

1

それはあなたが参照しているあなたのHTMLのどの部分は不明だが、あなたの入力の隣に<label>の要素を選択するために、あなたは$.prev()を使用する必要があります直前の要素の場合は$.next()、入力の直後の要素の場合は$.next()となります。

$.closest()は要素ではなく、あなたの<label>が隠されていない理由であるDOMにおける「物理的」に最も近い要素の閉じを選択します。選択されていません。

0

jQueryの最も近いメソッドは、オブジェクトの親でセレクタを検索することです。/nextAll

jQuery closest() not working for me (or i'm not working for it)

または、jQueryのAPIドキュメントを参照してください最も近い兄弟を検索するためのあなたはprevAllを見てみる必要があります prevAll - http://api.jquery.com/prevAll/ nextAll - http://api.jquery.com/nextAll/ と 最も近い - http://api.jquery.com/closest/

関連する問題