0

以下はコードスニペットです。ウィンドウのサイズと位置の両方に基づいて、クラス属性margin-bottomに影響を与えようとしています。高さ、幅などのすべてのインスタンスでこれが機能するようになりました...しかし、何らかの理由でmargin-bottomを使用すると、すべてのクラスが最後に来る JavaScript 関数のサイズになります。それが理にかなっているのかどうかわかりませんか?以下のコード:

//Javascript
var thisMB = null;
$("#Full").find(".t16").each(function () {
                thisMB = '1.25em';
            });
            $("#Full").find(".t8").each(function () {
                thisMB = '4.4175em';
            });

 $(this).css("margin-bottom", thisMB);

<!--html-->
             <div>      
                  <ul class="tlist">
                        <li class="theTeams t16">1 1upLeft upLeft upLeft </li>
                        <li class="theTeams t16">1 1upLeft upLeft upLeft </li>
                        <li class="theTeams t16">3 1 upLeft upLeft upLeft </li>
                        <li class="theTeams t16">4 1 upLeft upLeft upLeft </li>
                        <li class="theTeams t16">5 1 upLeft upLeft upLeft </li>
                        <li class="theTeams t16">6 1 upLeft upLeft upLeft </li>
                    </ul>
                </div>
                <div>
                    <ul class="tlist">
                        <li class="theTeams t8">1 upLeft upLeft upLeft </li>
                        <li class="theTeams t8">3 upLeft upLeft upLeft </li>
                        <li class="theTeams t8">5 upLeft upLeft upLeft </li>
                    </ul>
               </div>

基本的に、私の LI は、見つかった特定のクラス インスタンスではなく、後者の JavaScript 関数を使用します。したがって、.t16 は (たとえば) 14 の margin-bottom を持つ必要があり、.t8 は 42 である必要があります...どちらも 42 です。注文を移動すると、両方とも 14 になります。

アイデア?

4

2 に答える 2

2
var thisMB = null;
$("#Full").find(".t16").each(function () {
    thisMB = '1.25em';   <--- this assigns the same variable over and over again
 });
$("#Full").find(".t8").each(function () {
      thisMB = '4.4175em'; <--- so does this
});

$(this).css("margin-bottom", thisMB);   <--- this sets the element to thisMB = the final value.

変数を何度も割り当てていますが、ループの外で「this」に割り当てています。選択した要素 ( ) の値を設定する場合は、ループthisの内側にある必要があります。each().

于 2013-02-07T17:02:50.270 に答える
0

毎回異なる値で変数を 2 回設定しています。基本的にあなたはこれをやっています:

var thisMB = null;
thisMB = '1.25em';
thisMB = '4.4175em';

その後、thisMB の値を確認すると、最後の値セット「4.4175em」が得られます。

これがあなたが望むものだと思います:

$("#Full .t16").each(function () {
  $(this).css('margin-bottom', '1.25em');
});

$("#Full .t8").each(function () {
  $(this).css('margin-bottom', '4.4175em');
});

アップデート

少し短い:

$("#Full .t16").css('margin-bottom', '1.25em');
$("#Full .t8").css('margin-bottom', '4.4175em');
于 2013-02-07T17:06:15.153 に答える