jQuery每次调用其他函数

时间:2022-08-24 14:44:04

I am curious why this does not work. This is more hypothetical but I did run into this issue on a site that I am doing. I am trying to avoid a jQuery function inside a function. The this selector is the window instead of each of the boxes.

我很好奇为什么这不起作用。这是更假设的,但我确实在我正在做的网站上遇到了这个问题。我试图避免函数内部的jQuery函数。此选择器是窗口而不是每个框。

EDIT*** I DO UNDERSTAND HOW to do this doing the $(".box").each(function(){ // code here }); but is it not sloppy or bad practice functions inside of functions?

编辑***我理解如何做$(“。box”)。each(function(){// code here});但是它不是功能内部的草率或不好的练习功能吗?

http://jsfiddle.net/B8Yp3/3/

http://jsfiddle.net/B8Yp3/3/

CSS

CSS

.box {
    height: 50px;
    width: 50px;
    background: green;
    margin-bottom: 5px;
}

HTML

HTML

  <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>

Javascript

使用Javascript

 function changeMe(obj) {
        console.log(obj);
        $(obj).css("background", "blue");
    }

    $.each($(".box"), changeMe(this));

3 个解决方案

#1


3  

Second each paramater must be function - not function result:

其次,每个参数必须是函数 - 而不是函数结果:

$(".box").each(changeMe);

function changeMe(n, obj) { ........

#2


4  

When you call the $.each function,this still refers to the window object, as you noted. To have this refer to the jQuery object, you could call it in an anonymous function, which gets the proper scope applied.

当您调用$ .each函数时,这仍然指向窗口对象,如您所述。要让它引用jQuery对象,您可以在匿名函数中调用它,从而获得适当的范围。

$(".box").each(function(){ 
    changeMe(this); 
});

As a sidenote, it is absolutely not bad practice, sloppy, or anything negative whatsoever to call functions inside of other functions in Javascript. In fact, it's a cornerstone of Javascript programming (one of its most important concepts, callbacks, relies on functions inside of other functions). Functions are first-class citizens in Javascript, so they can be passed around, stuck inside other functions, and treated as any other object would be.

作为旁注,在Javascript中调用其他函数内部的函数绝对不是不好的做法,草率或任何负面的。事实上,它是Javascript编程的基石(其中一个最重要的概念,回调,依赖于其他函数内部的函数)。函数是Javascript中的一等公民,因此它们可以被传递,被困在其他函数中,并被视为任何其他对象。

You may be interested in reading more about functions in the MDN documentation. They explain a lot about functions being objects, nesting functions, closures and more, which may interest you if you wish to understand how functions are best used.

您可能有兴趣阅读有关MDN文档中的函数的更多信息。他们解释了很多关于函数的对象,嵌套函数,闭包等等,如果你想了解如何最好地使用函数,你可能会感兴趣。

#3


2  

Because the second parameter of $.each should be a function.

因为$ .each的第二个参数应该是一个函数。

If you pass changeMe(this), you are passing the return value of changeMe - you should wrap it into an anonymous function:

如果你传递changeMe(this),你传递的是changeMe的返回值 - 你应该将它包装成一个匿名函数:

 $.each($(".box"), function(){

    changeMe(this)

 });

#1


3  

Second each paramater must be function - not function result:

其次,每个参数必须是函数 - 而不是函数结果:

$(".box").each(changeMe);

function changeMe(n, obj) { ........

#2


4  

When you call the $.each function,this still refers to the window object, as you noted. To have this refer to the jQuery object, you could call it in an anonymous function, which gets the proper scope applied.

当您调用$ .each函数时,这仍然指向窗口对象,如您所述。要让它引用jQuery对象,您可以在匿名函数中调用它,从而获得适当的范围。

$(".box").each(function(){ 
    changeMe(this); 
});

As a sidenote, it is absolutely not bad practice, sloppy, or anything negative whatsoever to call functions inside of other functions in Javascript. In fact, it's a cornerstone of Javascript programming (one of its most important concepts, callbacks, relies on functions inside of other functions). Functions are first-class citizens in Javascript, so they can be passed around, stuck inside other functions, and treated as any other object would be.

作为旁注,在Javascript中调用其他函数内部的函数绝对不是不好的做法,草率或任何负面的。事实上,它是Javascript编程的基石(其中一个最重要的概念,回调,依赖于其他函数内部的函数)。函数是Javascript中的一等公民,因此它们可以被传递,被困在其他函数中,并被视为任何其他对象。

You may be interested in reading more about functions in the MDN documentation. They explain a lot about functions being objects, nesting functions, closures and more, which may interest you if you wish to understand how functions are best used.

您可能有兴趣阅读有关MDN文档中的函数的更多信息。他们解释了很多关于函数的对象,嵌套函数,闭包等等,如果你想了解如何最好地使用函数,你可能会感兴趣。

#3


2  

Because the second parameter of $.each should be a function.

因为$ .each的第二个参数应该是一个函数。

If you pass changeMe(this), you are passing the return value of changeMe - you should wrap it into an anonymous function:

如果你传递changeMe(this),你传递的是changeMe的返回值 - 你应该将它包装成一个匿名函数:

 $.each($(".box"), function(){

    changeMe(this)

 });