Jquery,切片失败时返回什么?
我在任何地方都找不到这个,如何检查切片是否返回仍在列表中的元素?就像 $('.class').slice(n-th,n-th);
当通过切片在整个返回类的索引中增加第 n 个变量时,如何知道何时切片返回无效索引?希望我解释得很好。
这是我想要完成的示例代码:
$(window).scroll(function() {
if(!($('embed').slice(p1,p2).show())){
$(window).unbind('scroll');
}
++p1;++p2;
}
另外,为什么我的事件没有解除绑定?
感谢您的帮助:)
I can't find this anywhere, how do you check to see if slice is returning elements still in the list? Like $('.class').slice(n-th,n-th);
When n-th variables are increased throughout the index of the return class by slicing, how do you know when slice returns on an invalid index? Hope I explained that well.
Here is the example code of what I'm trying to accomplish:
$(window).scroll(function() {
if(!($('embed').slice(p1,p2).show())){
$(window).unbind('scroll');
}
++p1;++p2;
}
Also, why is my event not unbinding?
Thanks for any assistance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需测试
slice().length
即可查看是否返回任何内容,或者您可以测试特定的length
。在您的情况下:
一般来说,您想要查看:
上面只是测试 1+ 元素。如果您要分割
N
元素,并且希望确保获得完整且完整的结果,只需修改为jQuery 的
.slice()
永远不会返回超出剩余元素末尾的内容。它足够聪明,知道何时停止。如果你给它一个超过元素数量的终点,它将返回存在的元素,然后它将停止而不会出现运行时错误。
Simply test
slice().length
to see if you got anything back, or you can test it for a specificlength
.In your case:
In general you want to look at:
The above simply tests for 1+ elements. If you're slicing off
N
elements and you want to make sure that you get a full and complete result, simply modify tojQuery's
.slice()
never returns things beyond the end of the remaining elements. It's smart enough to know when to stop.If you give it an end point that exceeds the number of elements, it'll return the elements that exist, then it'll stop without a runtime error.
如果传递错误的参数,Slice 只会返回一个空数组。
Slice just returns an empty array if you pass it bad arguments.