函数返回的jquery.index()不正确
帮我发疯。。。。函数返回的jquery.index()不正确,jquery,function,indexing,core,Jquery,Function,Indexing,Core,帮我发疯。。。。 .index()函数没有为我返回正确的索引,除非我把index()的整个概念都弄错了:) 这就是它背后的逻辑 <div class="sections">Section 1</div> <div class="sections">Section 2</div> <div class="sections">Section 3</div> <div class="sections">Section
.index()
函数没有为我返回正确的索引,除非我把index()的整个概念都弄错了:)
这就是它背后的逻辑
<div class="sections">Section 1</div>
<div class="sections">Section 2</div>
<div class="sections">Section 3</div>
<div class="sections">Section 4</div>
.sections{display:none;} //css
$('.sections:eq(1)').show();// shows sectiond 2 text
$('.sections:visible').index();// returns 2 I expected 1
第1节
第二节
第三节
第四节
.sections{display:none;}//css
$('.sections:eq(1)').show();//显示第2节文本
$('.sections:visible').index();//返回我期望的2个1
实际网站
<div class="sections">Section 1</div>
var section = $('.sections:visible').index();// returns 2
console.log($('.sections:visible').length); // returns 1
console.log($('.sections').length); // returns 1 as well since there is one section
window.location.hash = 'section-'+section;// url hash is #section-2
第1节
var section=$('.sections:visible')。index();//返回2
console.log($('.sections:visible').length);//返回1
console.log($('.sections').length);//由于有一个节,因此也返回1
window.location.hash='section-'+section;//url哈希为第2节
我想你搞错了什么。数组中的索引1表示整个集合的第二个元素。jquery的选择器总是返回一个数组
看看“.eq()”我想你搞错了什么。数组中的索引1表示整个集合的第二个元素。jquery的选择器总是返回一个数组
查看“.eq()”正确地阅读了
.index()
之后,我发现如果将列表指定为属性,它将解决问题
e、 g
换句话说,我们是说从$('.selections')
希望这对其他人有所帮助:)正确地阅读了
.index()
之后,我发现如果您将列表指定为属性,它将解决问题
e、 g
换句话说,我们是说从$('.selections')
希望这对其他人有帮助:)
换句话说,我们说的是从$('.selections')
没有参数,它给出选择器的索引。在辩论中,情况正好相反。正如。sections
不是的一部分。sections:可见
它给出-1(未找到)
因此,正确的答案是:
$('.sections').index($('.sections:visible'));
换句话说,我们说的是从$('.selections')
没有参数,它给出选择器的索引。在辩论中,情况正好相反。正如。sections
不是的一部分。sections:可见
它给出-1(未找到)
因此,正确的答案是:
$('.sections').index($('.sections:visible'));
它在Chrome上为我返回
1
,使用well return 1 for me在JSFIDLE上与我相同。在我的实际站点上,我只有一个.section
div,但为了更好地演示它,我在这里使用了倍数。我不知道那两个是从哪里来的。。。我还使用了.length返回1,这意味着没有任何其他具有相同类别的div在Chrome上使用well return 1 for me same with me on jsfiddle为我返回1
。在我的实际站点上,我只有一个.section
div,但为了更好地演示它,我在这里使用了倍数。我不知道那两个是从哪里来的。。。我使用了.length也返回1,这意味着没有任何其他div属于同一类lol我知道这里应该有一个愚蠢的meter,这样人们就知道我们的理解水平:)如果你查看.eq()的示例,你就会明白我的意思。所有三个示例实际上都标记了第三个元素。是的,我知道它的索引,这意味着它从0开始,而不是从1开始,因此.index()因此,函数也是基于0的,应该在逻辑中返回1,但它没有看到更新的问题。你应该检查一下,因为你低估了我的脚本知识lol:)顺便说一句,没有冒犯,但你有一点是的,我现在看到了。所以我必须道歉。哈哈,我知道我们这里应该有一个愚蠢的仪表,这样人们就知道我们的理解水平:)如果你查一下.eq()的例子,你就会明白我的意思。所有三个示例实际上都标记了第三个元素。是的,我知道它的索引,这意味着它从0开始,而不是从1开始,因此.index()因此,函数也是基于0的,应该在逻辑中返回1,但它没有看到更新的问题。你应该检查一下,因为你低估了我的脚本知识lol:)顺便说一句,没有冒犯,但你有一点是的,我现在看到了。所以我必须道歉。
$('.sections:visible').index($('.sections'));
$('.sections').index($('.sections:visible'));