parseInt不会像字符串一样处理和处理值

时间:2015-05-06 03:17:44

标签: javascript jquery

这就是我目前所拥有的:

var typeOrder = ['Land', 'Planeswalker', 'Creature', 'Instant', 'Sorcery', 'Enchantment', 'Artifact', 'Vanguard', 'Plane', 'Scheme', 'Summon', 'Tribal', 'Conspiracy', 'Phenomenon'];

$.each(typeOrder, function (_, order) {
    var cardQuantity = 0;
    var $sorted = $('<div>');

    // sort by types
    var $types = $('.card-line .types').filter(function () {
        return $(this).text() === '["'+order+'"]';
    });

    // title
    if($types.length !== 0) {
        cardQuantity += parseInt($types.siblings('.quantity').text(), 10); // quantity
        $sorted.prepend('<div class="title-gap"></div><h1>' + order + ' ('+ cardQuantity + ')</h1>');
    }

    $('#mainboard').append($sorted);
});

parseInt似乎无法在这里工作:

cardQuantity += parseInt($types.siblings('.quantity').text(), 10); // quantity

它一直给我像字符串一样的整数。例如,值为1 + 2,它给出了12而不是3.不确定为什么它不起作用。

编辑:FIDDLE

1 个答案:

答案 0 :(得分:3)

使用 text() 方法中的回调函数来迭代值

&#13;
&#13;
function SortCards() {
  var typeOrder = ['Land', 'Planeswalker', 'Creature', 'Instant', 'Sorcery', 'Enchantment', 'Artifact', 'Vanguard', 'Plane', 'Scheme', 'Summon', 'Tribal', 'Conspiracy', 'Phenomenon'];

  $.each(typeOrder, function(_, order) {
    var cardQuantity = 0;
    var $sorted = $('<div>');

    // sort by types
    var $types = $('.card-line .types').filter(function() {
      return $(this).text() === '["' + order + '"]';
    });

    // land sort
    if (order === 'Land') {
      $types.parent().sort(function(a, b) {
        var landTypeA = $(a).find('.supertypes').text();
        var landTypeB = $(b).find('.supertypes').text();

        return (landTypeA == '["Basic"]' && landTypeB != '["Basic"]') ? 0 : (landTypeA != '["Basic"]' && landTypeB == '["Basic"]') ? 1 : -1;
      }).appendTo($sorted);
    }

    // secondary sort by cmc
    $types.parent().sort(function(a, b) {
      var cmcA = +$(a).find('.cmc').text();
      var cmcB = +$(b).find('.cmc').text();

      return cmcA > cmcB ? 1 : (cmcA < cmcB ? 0 : -1);
    }).appendTo($sorted);

    // title
    if ($types.length !== 0) {

      $types.siblings('.quantity').text(function(i, v) {
        cardQuantity += parseInt(v, 10);
      }); // quantity
      $sorted.prepend('<div class="title-gap"></div><h1>' + order + ' (' + cardQuantity + ')</h1>');
    }

    $('#mainboard').append($sorted);
  });
}

SortCards();
&#13;
.title-gap {
  width: 100%;
  height: 10px;
}
.card-quantity {
  display: inline;
}
.card-name {
  display: inline;
}
.quantity {
  display: none;
}
.card-line div.types {
  display: none;
}
.card-line div.supertypes {
  display: none;
}
.card-line div.rarity {
  display: none;
}
.card-line div.colors {
  display: none;
}
.card-line div.name {
  display: none;
}
.card-line div.cmc {
  display: none;
}
.card-line div.set {
  display: none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="mainboard">
  <div class="card-line">
    <div class="card-quantity">1x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/ISD/kessig wolf run.jpg">Kessig Wolf Run</div>
    <div class="quantity">1</div>
    <div class="types">["Land"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Rare</div>
    <div class="colors"></div>
    <div class="name">kessig wolf run</div>
    <div class="cmc"></div>
    <div class="set">ISD</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">1x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/AVR/alchemist's refuge.jpg">Alchemist's Refuge</div>
    <div class="quantity">1</div>
    <div class="types">["Land"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Rare</div>
    <div class="colors"></div>
    <div class="name">alchemist's refuge</div>
    <div class="cmc"></div>
    <div class="set">AVR</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">4x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/DIS/hallowed fountain.jpg">Hallowed Fountain</div>
    <div class="quantity">4</div>
    <div class="types">["Land"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Rare</div>
    <div class="colors"></div>
    <div class="name">hallowed fountain</div>
    <div class="cmc"></div>
    <div class="set">DIS</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">4x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/RAV/temple garden.jpg">Temple Garden</div>
    <div class="quantity">4</div>
    <div class="types">["Land"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Rare</div>
    <div class="colors"></div>
    <div class="name">temple garden</div>
    <div class="cmc"></div>
    <div class="set">RAV</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">4x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/M10/sunpetal grove.jpg">Sunpetal Grove</div>
    <div class="quantity">4</div>
    <div class="types">["Land"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Rare</div>
    <div class="colors"></div>
    <div class="name">sunpetal grove</div>
    <div class="cmc"></div>
    <div class="set">M10</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">1x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/GPT/steam vents.jpg">Steam Vents</div>
    <div class="quantity">1</div>
    <div class="types">["Land"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Rare</div>
    <div class="colors"></div>
    <div class="name">steam vents</div>
    <div class="cmc"></div>
    <div class="set">GPT</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">2x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/AVR/cavern of souls.jpg">Cavern of Souls</div>
    <div class="quantity">2</div>
    <div class="types">["Land"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Rare</div>
    <div class="colors"></div>
    <div class="name">cavern of souls</div>
    <div class="cmc"></div>
    <div class="set">AVR</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">4x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/RAV/overgrown tomb.jpg">Overgrown Tomb</div>
    <div class="quantity">4</div>
    <div class="types">["Land"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Rare</div>
    <div class="colors"></div>
    <div class="name">overgrown tomb</div>
    <div class="cmc"></div>
    <div class="set">RAV</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">4x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/ISD/hinterland harbor.jpg">Hinterland Harbor</div>
    <div class="quantity">4</div>
    <div class="types">["Land"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Rare</div>
    <div class="colors"></div>
    <div class="name">hinterland harbor</div>
    <div class="cmc"></div>
    <div class="set">ISD</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">1x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/CON/nicol bolas, planeswalker.jpg">Nicol Bolas, Planeswalker</div>
    <div class="quantity">1</div>
    <div class="types">["Planeswalker"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Mythic Rare</div>
    <div class="colors">["Blue","Black","Red"]</div>
    <div class="name">nicol bolas, planeswalker</div>
    <div class="cmc">8</div>
    <div class="set">CON</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">2x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/AVR/griselbrand.jpg">Griselbrand</div>
    <div class="quantity">2</div>
    <div class="types">["Creature"]</div>
    <div class="supertypes">["Legendary"]</div>
    <div class="rarity">Mythic Rare</div>
    <div class="colors">["Black"]</div>
    <div class="name">griselbrand</div>
    <div class="cmc">8</div>
    <div class="set">AVR</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">2x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/RTR/angel of serenity.jpg">Angel of Serenity</div>
    <div class="quantity">2</div>
    <div class="types">["Creature"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Mythic Rare</div>
    <div class="colors">["White"]</div>
    <div class="name">angel of serenity</div>
    <div class="cmc">7</div>
    <div class="set">RTR</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">1x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/M13/thragtusk.jpg">Thragtusk</div>
    <div class="quantity">1</div>
    <div class="types">["Creature"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Rare</div>
    <div class="colors">["Green"]</div>
    <div class="name">thragtusk</div>
    <div class="cmc">5</div>
    <div class="set">M13</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">2x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/M14/fog.jpg">Fog</div>
    <div class="quantity">2</div>
    <div class="types">["Instant"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Common</div>
    <div class="colors">["Green"]</div>
    <div class="name">fog</div>
    <div class="cmc">1</div>
    <div class="set">M14</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">4x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/RTR/sphinx's revelation.jpg">Sphinx's Revelation</div>
    <div class="quantity">4</div>
    <div class="types">["Instant"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Mythic Rare</div>
    <div class="colors">["White","Blue"]</div>
    <div class="name">sphinx's revelation</div>
    <div class="cmc">3</div>
    <div class="set">RTR</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">4x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/pMEI/supreme verdict.jpg">Supreme Verdict</div>
    <div class="quantity">4</div>
    <div class="types">["Sorcery"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Special</div>
    <div class="colors">["White","Blue"]</div>
    <div class="name">supreme verdict</div>
    <div class="cmc">4</div>
    <div class="set">pMEI</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">2x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/AVR/temporal mastery.jpg">Temporal Mastery</div>
    <div class="quantity">2</div>
    <div class="types">["Sorcery"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Mythic Rare</div>
    <div class="colors">["Blue"]</div>
    <div class="name">temporal mastery</div>
    <div class="cmc">7</div>
    <div class="set">AVR</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">2x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/DKA/increasing ambition.jpg">Increasing Ambition</div>
    <div class="quantity">2</div>
    <div class="types">["Sorcery"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Rare</div>
    <div class="colors">["Black"]</div>
    <div class="name">increasing ambition</div>
    <div class="cmc">5</div>
    <div class="set">DKA</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">4x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/M13/farseek.jpg">Farseek</div>
    <div class="quantity">4</div>
    <div class="types">["Sorcery"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Common</div>
    <div class="colors">["Green"]</div>
    <div class="name">farseek</div>
    <div class="cmc">2</div>
    <div class="set">M13</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">4x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/M13/ranger's path.jpg">Ranger's Path</div>
    <div class="quantity">4</div>
    <div class="types">["Sorcery"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Common</div>
    <div class="colors">["Green"]</div>
    <div class="name">ranger's path</div>
    <div class="cmc">4</div>
    <div class="set">M13</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">1x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/M13/omniscience.jpg">Omniscience</div>
    <div class="quantity">1</div>
    <div class="types">["Enchantment"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Mythic Rare</div>
    <div class="colors">["Blue"]</div>
    <div class="name">omniscience</div>
    <div class="cmc">10</div>
    <div class="set">M13</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">3x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/RTR/chromatic lantern.jpg">Chromatic Lantern</div>
    <div class="quantity">3</div>
    <div class="types">["Artifact"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Rare</div>
    <div class="colors"></div>
    <div class="name">chromatic lantern</div>
    <div class="cmc">3</div>
    <div class="set">RTR</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">2x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/M13/gilded lotus.jpg">Gilded Lotus</div>
    <div class="quantity">2</div>
    <div class="types">["Artifact"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Rare</div>
    <div class="colors"></div>
    <div class="name">gilded lotus</div>
    <div class="cmc">5</div>
    <div class="set">M13</div>
  </div>
  <div class="card-line">
    <div class="card-quantity">1x</div>
    <div class="card-name vcard" data-vcard-x="mouse" data-image-path="http://goblinhammer.dev/images/cards/5DN/door to nothingness.jpg">Door to Nothingness</div>
    <div class="quantity">1</div>
    <div class="types">["Artifact"]</div>
    <div class="supertypes"></div>
    <div class="rarity">Rare</div>
    <div class="colors"></div>
    <div class="name">door to nothingness</div>
    <div class="cmc">5</div>
    <div class="set">5DN</div>
  </div>
</div>
&#13;
&#13;
&#13;