admin管理员组文章数量:1221774
When I'm trying to store all <a>
elements as objects in array (using $('a')
), and then get the position of each of them, it doesn't work.
years = $('a');
for(i=0;i< years.length;i++){
if(years[i].position().top > year.position().top){
}
else{
}
}
Console says:
Uncaught TypeError: Object file:///Users/.../index.html# has no method 'position'
When I do it with single element selected by class name instead of tag name, everything works fine.
What am I doing wrong ?
When I'm trying to store all <a>
elements as objects in array (using $('a')
), and then get the position of each of them, it doesn't work.
years = $('a');
for(i=0;i< years.length;i++){
if(years[i].position().top > year.position().top){
}
else{
}
}
Console says:
Uncaught TypeError: Object file:///Users/.../index.html# has no method 'position'
When I do it with single element selected by class name instead of tag name, everything works fine.
What am I doing wrong ?
Share Improve this question edited Aug 2, 2021 at 14:39 isherwood 61.1k16 gold badges120 silver badges168 bronze badges asked May 16, 2012 at 22:08 MichaelMichael 7436 silver badges18 bronze badges 1- 3 Should we guess your JavaScript? Or could you possibly help us to help you, by showing what you've tried? – David Thomas Commented May 16, 2012 at 22:10
2 Answers
Reset to default 15Use this instead:
$("a").each(function() {
var pos = $(this).position();
if (pos.top > year.position().top) {
// hurray
}
});
Also what is the value of year
? I prefer to name jQuery objects like this: var $year = $("#year");
The $
helps you remember it's a jQuery object.
You can do the following:
var arr = [], elems = $('a');
for(var i = 0; i < elems.length; i++){
arr[i] = elems[i];
}
本文标签: javascriptHow do I iterate over the results of a jQuery selectorStack Overflow
版权声明:本文标题:javascript - How do I iterate over the results of a jQuery selector? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739273551a2155925.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论