Jquery 计算具有特定id的跨度的跨度数

Jquery 计算具有特定id的跨度的跨度数,jquery,Jquery,如何计算span-s的数量,直到具有特定id的span 例如: id为“a5”的第六个span是第六个span <table border="3"> <tr> <td><span id="a2" class="arrow_icon a6" isClicked="0"></span></td> <td><span id="a1" class="arrow_icon a1

如何计算span-s的数量,直到具有特定id的span

例如:

id为“a5”的第六个span是第六个span

<table border="3">
    <tr>
        <td><span id="a2" class="arrow_icon a6" isClicked="0"></span></td>
        <td><span id="a1" class="arrow_icon a1" isClicked="0"></span></td>
        <td><span id="a3" class="arrow_icon a11" isClicked="0"></span></td>
        <td><span id="a4" class="arrow_icon a16" isClicked="0"></span></td>
        <td><span id="a6" class="arrow_icon a2" isClicked="0"></span></td>
    </tr>
    <tr>
        <td><span id="a5" class="arrow_icon a21" isClicked="0"></span></td>
        <td><span id="a7" class="arrow_icon a7" isClicked="0"></span></td>
        <td><span id="a8" class="arrow_icon a12" isClicked="0"></span></td>
        <td><span id="a9" class="arrow_icon a17" isClicked="0"></span></td>
        <td><span id="a10" class="arrow_icon a22" isClicked="0"></span></td>
    </tr>
</table>

感谢您的帮助

使用return false提前退出.each循环

var count = 0;
var toId = "a5";
$("span").each(function(){
   count++;
   if(this.id == toId) {
      return false;
   }
});
console.log(count);
或者使用一行


使用return false提前退出.each循环

var count = 0;
var toId = "a5";
$("span").each(function(){
   count++;
   if(this.id == toId) {
      return false;
   }
});
console.log(count);
或者使用一行

您不能使用,因为这会查看同级元素,并且示例中并非所有的span元素都是同级元素。将选择器放入
$(…)
中应该会生成一个jQuery对象,该对象尊重元素在DOM中的顺序,因此可以:

var count = 0;
$("span").each(function(i) {
    count++;
    if (this.id === "a5") {return false;} // breaks out of loop when the id is found
});

console.log(count);
您不能使用,因为这会查看同级元素,并且示例中并非所有的span元素都是同级元素。将选择器放入
$(…)
中应该会生成一个jQuery对象,该对象尊重元素在DOM中的顺序,因此可以:

var count = 0;
$("span").each(function(i) {
    count++;
    if (this.id === "a5") {return false;} // breaks out of loop when the id is found
});

console.log(count);

只需使用
.each()
遍历所有跨度,并使用索引(循环迭代次数)作为计数器:

var c;
$("span").each(function(index, element) {
    if(element.id == "a5") {
        c = index; // set counter to current index value
        return false; // break the each loop
    }
});
alert(c);

只需使用
.each()
循环所有跨距,并使用索引(循环迭代次数)作为计数器:

var c;
$("span").each(function(index, element) {
    if(element.id == "a5") {
        c = index; // set counter to current index value
        return false; // break the each loop
    }
});
alert(c);

不能使用nextUntil(),因为他们不是兄弟姐妹。这是我能想到的最好的办法

var a5index;
$('table span').each(function(idx){
  if( $(this).is('#a5')){
    a5Index = idx;
    return true; // No reason to keep going after we find it...
  }
});
不能使用nextUntil(),因为他们不是兄弟姐妹。这是我能想到的最好的办法

var a5index;
$('table span').each(function(idx){
  if( $(this).is('#a5')){
    a5Index = idx;
    return true; // No reason to keep going after we find it...
  }
});

首先,您的初始选择在第一个
中只包含一个span元素。因此,您应该将其改为
$(“span”)…

其次,nextUntil函数在所选项目的同级中搜索,只需查看jQuery文档即可确认:

这意味着,您的跨度集合将更改为。。。零。为什么?他们的直系父母是
标记,它们只包含一个span,因此他们中没有一个真正有兄弟姐妹

补救办法很简单:

var count = 0;

$("span").each(function(i, e){
    if(!$(this).is("#a5"))
        count++; //count it up
    else
        return false; //exit the each function
});

alert(count); //alerts '5', as it should
它工作的主要原因是选择已经按照DOM中显示的方式进行了排序。否则就很难用它做任何事情


干杯

首先,您的初始选择在第一个
中只包含一个span元素。因此,您应该将其改为
$(“span”)…

其次,nextUntil函数在所选项目的同级中搜索,只需查看jQuery文档即可确认:

这意味着,您的跨度集合将更改为。。。零。为什么?他们的直系父母是
标记,它们只包含一个span,因此他们中没有一个真正有兄弟姐妹

补救办法很简单:

var count = 0;

$("span").each(function(i, e){
    if(!$(this).is("#a5"))
        count++; //count it up
    else
        return false; //exit the each function
});

alert(count); //alerts '5', as it should
它工作的主要原因是选择已经按照DOM中显示的方式进行了排序。否则就很难用它做任何事情


干杯

这个
放进
$(…)
只是为了得到id属性,这有点浪费。只需使用
这个。id
yea正在用它做其他事情,需要包装,只是没有想到要打开它。将
这个
放入
$(…)
只是为了获得id属性是一种浪费。只需使用
this.id
yea正在用它做其他事情,需要包装,只是没想到要打开它。