创建for循环以检查跨越id

时间:2013-11-17 22:46:29

标签: javascript jquery

我正在尝试创建一个循环七次的for循环。它将检查我的外部跨度id以查看最里面的跨度是否包含静态数字1。 这就是我试过的事情

for (var i=0; i<8; i++) {
   var mySpans = $('mySpan'+i);         

    if ($(this).find('mySpans.span').text() == '1') {
        alert("found");       }
}

HTML

<span class="spanClass" id="mySpan1">
  <div>
    <span class="innerSpanClass"> </span>
  </div>
</span>

<span class="spanClass" id="mySpan2">
  <div>
    <span class="innerSpanClass"> </span>
  </div>
</span>

<span class="spanClass" id="mySpan3">
  <div>
    <span class="innerSpanClass"> </span>
  </div>
</span>

<span class="spanClass" id="mySpan4">
  <div>
    <span class="innerSpanClass"> </span>
  </div>
</span>

<span class="spanClass" id="mySpan5">
  <div>
    <span class="innerSpanClass"> </span>
  </div>
</span>

<span class="spanClass" id="mySpan6">
  <div>
    <span class="innerSpanClass">1</span>
  </div>
</span>

<span class="spanClass" id="mySpan7">
  <div>
    <span class="innerSpanClass">2</span>
  </div>
</span>

感谢一堆

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,您可以先使用选择器抓住所有跨度,然后过滤所需的内容,并计算找到的项目:

var $result = $('.spanClass .innerSpanClass').filter(function() {
  return $.trim($(this).text()) == '1';
});

if ($result.length) alert('found');