jQuery 中的 function() 是什么意思?这和 $(this) 有什么区别

发布于 2024-11-16 15:17:55 字数 1242 浏览 0 评论 0原文

问题1

我刚刚开始学习jQuery,让我困惑的一件事是其中带有参数的function(),即:function(e).

[示例 1]

 $("#rating a").click(function(e){
     // stop normal link click
     e.preventDefault();
 }

function(e) 中的 e 是什么?

[示例 2]

$.post("test.php", function(data) {
    alert("Data Loaded: " + data);
});

你如何知道数据是什么?我假设 data 与示例 1 中的 e 不同。

我很困惑为什么 < code>function() 在不同场景下是不同的...


问题2 由于我们正在讨论基本的 jQuery,所以这里有另一个 jQuery 问题。 this$(this) 有什么区别?

[示例 3]

$(document).ready(function() {
 // use this to reset several forms at once
$("#reset").click(function() {
    $("form").each(function() {
        this.reset();
    });
});

[示例 4]

   $("a").hover(function(){
       $(this).parents("p").addClass("highlight");
   },function(){
       $(this).parents("p").removeClass("highlight");
   });

});

Question 1

I've just started learning jQuery and one thing that puzzles me is function() with a parameter in it, ie: function(e).

[Example 1]

 $("#rating a").click(function(e){
     // stop normal link click
     e.preventDefault();
 }

What is e in function(e)?

[Example 2]

$.post("test.php", function(data) {
    alert("Data Loaded: " + data);
});

How do you know what data is? I assume data is different from e in example 1.

I'm confused as to why function() is different in different senarios...


Question 2
Since we are talking about basic jQuery, here's another jQuery question. What is the difference between this and $(this)?

[Example 3]

$(document).ready(function() {
 // use this to reset several forms at once
$("#reset").click(function() {
    $("form").each(function() {
        this.reset();
    });
});

[Example 4]

   $("a").hover(function(){
       $(this).parents("p").addClass("highlight");
   },function(){
       $(this).parents("p").removeClass("highlight");
   });

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷鸟归林 2024-11-23 15:17:56

您可以在 JavaScript 中通过两种方式之一声明函数。

这样:

function MyFunction() {
     //Functiony stuff
}

这样:

var MyFunction = function() {

};

使用第二种方法,您可以将该函数作为参数传递给其他函数。然后接收它的那些函数可以为其提供参数。如果您未在函数中定义这些参数,则无法访问它们。这里还有一点:

var MyFunction1 = function(message) {
      alert(message);
};

var MyFunction2 = function(alertFunction) {
      if(alertFunction != null)
           alertFunction("Here's another method!");
};

//You  can then call MyFunction and pass it arguments, even if that argument is a function.

MyFunction2(MyFunction1);

如果您没有在 MyFunction1 中定义消息变量,您将永远不会收到通过 MyFunction2 传递给它的消息。希望我没有让事情变得比需要的更复杂......基本上,你可以传递函数,如果你不提供方法将传递给它的变量,你就不能使用它们。当您说以下内容时,这就是您正在做的事情:

$("MySelector").click(function(e) {
    //Stuff here
});

您正在向点击函数发送一个匿名函数。如果您不提供它将传递给您的变量,您将无法使用它们。

很久以前,当我意识到你可以做到这一点时,它极大地改变了我编写 JavaScript 的方式。我相信它可能也会为你做同样的事情。

You can declare functions in one of two ways in JavaScript.

This way:

function MyFunction() {
     //Functiony stuff
}

And this way:

var MyFunction = function() {

};

Using the second method, you can then pass that function to other functions as an argument. Those functions that receive it can then provide it arguments. If you don't define those arguments in your function, then you can't access them. Here's a bit more:

var MyFunction1 = function(message) {
      alert(message);
};

var MyFunction2 = function(alertFunction) {
      if(alertFunction != null)
           alertFunction("Here's another method!");
};

//You  can then call MyFunction and pass it arguments, even if that argument is a function.

MyFunction2(MyFunction1);

Had you not defined the message variable in MyFunction1, you would have never gotten the message that was passed to it via MyFunction2. Hope I didn't make that more complicated than it needed to be... Basically, you can pass functions around, and if you don't provide the variables that methods will pass to it, you can't use them. This is what you're doing when you say something like:

$("MySelector").click(function(e) {
    //Stuff here
});

You're sending an anonymous function to the click function. If you don't provide for the variables that it will pass to you, you can't use them.

Once I realized you could do this a long time ago, it changed the way I wrote JavaScript quite a lot. I'm sure it'll probably do the same for you.

只等公子 2024-11-23 15:17:56

在第一个示例中:

 $("#rating a").click(function(e){
     // stop normal link click
     e.preventDefault();
 }

e事件对象。它的 PreventDefault 方法可防止所使用事件的默认值。在这种情况下,单击将不会产生任何效果。它的用法类似于return false;但某些浏览器可能有不同的操作。这对所有人都有效。

至于 this 与 $(this)

在函数内部,如果 this 引用触发事件的 DOM 元素,则 $(this)仍然是触发事件的 DOM 元素,只是通过将 $() 包裹在 jQuery 元素中。

检查这个问题jQuery:'$(this)'和'this'之间有什么区别?

In your first example:

 $("#rating a").click(function(e){
     // stop normal link click
     e.preventDefault();
 }

e is the event object. It's preventDefault method prevents the default for the used event. In this case click will not have any effect. It is used like return false; but some browser may have different actions. This works for all.

As for this vs $(this)

While inside a function, if this refers to the DOM element that triggered the event, $(this) is still the DOM element that triggered the event, only it is wrapped in a jQuery element by wrapping a $() around it.

check this question on SO jQuery: What's the difference between '$(this)' and 'this'?

晨光如昨 2024-11-23 15:17:56

function(x) { ... }

是一个匿名函数(即您没有给它一个明确的名称)作为参数传递给这些函数(在您的示例中,作为参数传递给 click() 和 post() )。这通常用于回调。 click() 函数执行一些操作,然后调用匿名函数作为对某些内容(在本例中为单击事件)的响应。

x 表示该匿名函数采用一个参数,并且其在函数作用域内的名称将为 x。您可以将其命名为任何您想要的名称(在一个示例中它是 e,在另一个示例中它是 data,但在这两种情况下都不必使用这些名称)。一般来说,你可以让它接受任意多个参数,例如 function(x, y, z) { return (x + y + z);但是,在您给出的示例中,jQuery 正在寻找某个原型。

function(x) { ... }

is an anonymous function (i.e. you're not giving it an explicit name) being passed as an argument to those functions (in your examples, passed as an argument to click() and post()). This is usually used for callbacks. The click() function does some stuff and then later on down the line, your anonymous function is invoked as a response to something (a click event, in this case).

The x indicates that this anonymous function takes one parameter and its name within the function's scope will be x. You can name it whatever you want (in one example it's e, in another it's data, but in neither case does it have to use those names). In general, you could make it take as many parameters as you want, like function(x, y, z) { return (x + y + z); } but, in the examples you gave, jQuery is looking for a certain prototype.

糖果控 2024-11-23 15:17:55

函数(e) ->这是将事件对象作为参数传递。如果没有 e,您就无法访问事件对象。

“this”是与您正在做的任何事情相关的对象。如果你像 $(this) 一样包装“this”,那么你就可以访问 jQuery 提供的所有扩展。更清楚地说,“this”通常是一个 DOM 对象,$(this) 是一个 jQuery 对象。

function(e) -> this is passing in the event object as a parameter. Without e you just don't have access to the event object.

"this" is the object in context to whatever you're doing. If you wrap "this" like $(this) then you have access to all the extensions that jQuery gives you. To be more clear, "this" is usually a DOM object, $(this) is a jQuery object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文