admin管理员组文章数量:1400503
I want to loop through a set of <a>
's in an unordered list.
In jQuery, I'd do it this way:
$("#list ul li a").each(function (x) {
// Do Stuff
});
It's probably a simple question, but how do I pass a variable through as that id
shown in the function there? I'd imagine it is something like:
var myVar = 'foo';
$("#list ul li a").each(myVar, function (x) {
// Do Stuff
console.log(x);
});
Console out: foo
.
But that doesnt seem to work, how do I do this?
I want to loop through a set of <a>
's in an unordered list.
In jQuery, I'd do it this way:
$("#list ul li a").each(function (x) {
// Do Stuff
});
It's probably a simple question, but how do I pass a variable through as that id
shown in the function there? I'd imagine it is something like:
var myVar = 'foo';
$("#list ul li a").each(myVar, function (x) {
// Do Stuff
console.log(x);
});
Console out: foo
.
But that doesnt seem to work, how do I do this?
Share Improve this question edited Mar 24, 2015 at 13:44 Chud37 asked Mar 24, 2015 at 13:42 Chud37Chud37 5,03714 gold badges68 silver badges129 bronze badges 5- $(this).attr('id'); will refer to the current element's id, so maybe this can be tha answer to your problem if i get your question correct. – Balázs Varga Commented Mar 24, 2015 at 13:43
-
4
It's not clear what you mean by "pass a variable through". Code in the callback you pass to
.each()
can "see" local variables like "myVar" in the surrounding code. What is it that you want to do exactly? – Pointy Commented Mar 24, 2015 at 13:43 - @Pointy Can it really? I've had numerous times when .each and .ajax cannot see my local variables. – Chud37 Commented Mar 24, 2015 at 13:44
-
2
@Chud37 it depends on where they are declared. In your example,
console.log(myVar)
in theeach()
would work fine, though. – Rory McCrossan Commented Mar 24, 2015 at 13:46 - @Chud37 Take a look at this: JavaScript Scope And Closures explained – Praxis Ashelin Commented Mar 24, 2015 at 13:49
3 Answers
Reset to default 7You don't need to pass variable on each(myVar, function(x)
. You can use myVar
inside each
with no problem.
var myVar = 'foo';
$("#list ul li a").each(function (x) {
// Do Stuff
console.log(x); // element index
console.log(myVar); //foo
});
Let's see what is wrong with this code.
var myVar = 'foo';
$("#list ul li a").each(myVar, function (x) {
// Do Stuff
console.log(x);
});
You will get:
Uncaught TypeError: undefined is not a function
Because exects that first parameter to be a function
and it's a variable.
Also function(x)
will never be called because each
take only 1 parameter.
Here is the solution,
just use "this" keyword like this:
var myVar = 'foo';
$("#list ul li a").each(function () {
console.log($(this).attr('id'));
});
Thanks
var myVar = 'foo';
$("#list ul li a").each(myVar, function (id) {
// Do Stuff
console.log(id.attr("id"));
});
本文标签: javascriptjquery each function and passing variablesStack Overflow
版权声明:本文标题:javascript - jquery .each function and passing variables - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744255561a2597464.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论