2016-05-06 3 views
-1

一部の入力フィールドが水平に1行に整列したフォームがあります。エラーがあった場合、エラーが発生した入力要素にスクロールしたいが、コンテナを設定できない#preferences scrollLeft。はjavascriptでdiv.scrollLeftを設定できません

<form id="preferences"> 
    <input type="text" value="" placeholder="" id="new_username" name="username"/> 

    <input type="text" value="" placeholder="aww:/ add an email" id="email" name="email"/> 

    <input type="password" value="" placeholder="password" id="old_password" name="password"/> 
    <input type="password" value="" placeholder="new password" id="new_password" name="new_password"/> 

    <input type="submit" value="update"> 
</form> 

#preferences { 
    text-align: center 
    margin: 10px 0; 
    white-space: nowrap; 
    overflow-x: auto; 
} 

var email = document.getElementById('email'), 
    preferences = document.getElementById('preferences'); 

preferences.scrollLeft = email.offsetLeft; 
+1

あなたが設定することはできません何を意味するのですか?何かエラーが出るのですか、まったく効果がありませんか? '#preferences'とは何ですか?どのようなスタイルを持っていますか?より具体的にマークアップとCSSを表示してください。 – Gabriel

+0

何も起こらず、エラーもありません。更新された質問をご覧ください。 @Gabriel –

答えて

0

コンテナー(フォーム)が定義された幅を有していないので、それだけ、横方向に膨張します。オーバーフローしていないため、スクロールできません。ただし、幅が固定されているか、本体であれば親をスクロールできます。

参照:

preferences.scrollLeft = 250; //it works...
#preferences { 
 
    text-align: center 
 
    margin: 10px 0; 
 
    white-space: nowrap; 
 
    overflow-x: auto; 
 
    width: 300px; /*if you set the width*/ 
 
}
<form id="preferences"> 
 
    <input type="text" value="" placeholder="" id="new_username" name="username"/> 
 

 
    <input type="text" value="" placeholder="aww:/ add an email" id="email" name="email"/> 
 

 
    <input type="password" value="" placeholder="password" id="old_password" name="password"/> 
 
    <input type="password" value="" placeholder="new password" id="new_password" name="new_password"/> 
 

 
    <input type="submit" value="update"> 
 
</form>

+0

element.offsetLeftを使用しても機能しません。なぜなら、位置を取得する必要があり、環境設定は幅が100%である必要があるからです。 –

+0

それは正しいです、それはまさに私が言ったものです。私の例では300pxを使用しましたが、100%にすることもできますが、ポイントは固定幅にすることです。 – Gabriel

+0

私にとってはうまくいかない。別の問題があるはずです。 –

関連する問題