0

TwitterBootstrapのレスポンシブグリッドに基づいて関数を作成しています。.row-fluidコンテナとして、およびspans行内に収まるノードとして使用します。各行には、最大12個の「スパン」を含めることができます。

.row-fluid関数でクラスを持つ要素を検索し、その子ノードを検索し、クラス名を取得し、クラス名から「スパン」を削除して(数値のみを残して)、それらの数値を合計したいと思います。結果が12より大きい場合は、最大数を12に等しくなるまで縮小します。

複雑に聞こえますが、うまくいけば、私はそれほど遠くないです。これが私が今のところいるところです:

$('.row-fluid').each(function() {
    var spanned = $(this).children('div[class*=span]').each(function() {
        var total = 0, nums = $(this).attr('class').match(/\d+/);
        nums.each(function() {
            total += this;
        }
        console.log(total);
    }
    );
    console.log("break");
}
);

現在、これは数字だけでなく全体の要素をログに記録しているので、どこが間違っているのか/ここから何をすべきかについて少し迷っています。何かアドバイス?

編集:構造はこれに似ています:

<div class="row-fluid">
  <div class="span5">

  </div>
  <div class="span4">


  </div>
  <div class="span2">

  </div> //Function should check if the 3 above spans <= 12
  <div class="row-fluid">
      <div class="span8"> //Function should see this and...
        <div class="row-fluid">
          <div class="span6">Fluid 6</div>
          <div class="span6">Fluid 6</div>
        </div>
      </div>
      <div class="span6">Fluid 6</div> //...this and see that they DON'T equal 12, then subtract 2 from the bigger span so that they DO equal 12
    </div>
  </div>
</div>
4

2 に答える 2

1

var total = 0, nums = $(this).attr('class').match(/\d+/);

醜い行-人間の蜂の解析が簡単なため、各変数を別々の行で定義します;]

nums = $(this).attr('class').match(/\d+/);

これは文字列であり、数値ではありません。代わりに次のようにしてください。

var numString = $(this).attr('class').match(/\d+/);
var num = parseInt(numString);

ここで何が起こるかわかりません:

nums.each(function() {

しかし、私が想定する良いことは何もありません...各関数はjQuery要素用であるため、jQueryはnumsをjQueryオブジェクトとして扱う可能性があります

total += this;

通常、各パラメーター関数内では、「this」キーワードは一種のjqueryセレクターであるため、uは数値ではなく要素を取得します。

spans.each()の外側で合計変数を宣言する必要があると思います。これは、uが各反復後にクリアされるためです+次のように実行します。

total += num;

上で述べたように、uがnumStringをnumberに解析すると仮定します。

于 2013-01-22T10:24:14.357 に答える
1

私は大げさな推測をします、これはあなたがやろうとしていることです:

$('.row-fluid').each(function(i,ele) {
    var total = 0;
    $(ele).children().each(function(i2, spans) {
        if (spans.className.indexOf('span') != -1) {
             total += parseInt(spans.className.replace(/[A-Za-z$-]/g, ""),10);
        }
    });
});
于 2013-01-22T10:25:49.083 に答える