jQuery 中的 function() 是什么意思?这和 $(this) 有什么区别
问题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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在 JavaScript 中通过两种方式之一声明函数。
这样:
这样:
使用第二种方法,您可以将该函数作为参数传递给其他函数。然后接收它的那些函数可以为其提供参数。如果您未在函数中定义这些参数,则无法访问它们。这里还有一点:
如果您没有在 MyFunction1 中定义消息变量,您将永远不会收到通过 MyFunction2 传递给它的消息。希望我没有让事情变得比需要的更复杂......基本上,你可以传递函数,如果你不提供方法将传递给它的变量,你就不能使用它们。当您说以下内容时,这就是您正在做的事情:
您正在向点击函数发送一个匿名函数。如果您不提供它将传递给您的变量,您将无法使用它们。
很久以前,当我意识到你可以做到这一点时,它极大地改变了我编写 JavaScript 的方式。我相信它可能也会为你做同样的事情。
You can declare functions in one of two ways in JavaScript.
This way:
And this way:
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:
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:
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.
在第一个示例中:
e
是 事件对象。它的 PreventDefault 方法可防止所使用事件的默认值。在这种情况下,单击将不会产生任何效果。它的用法类似于return false;但某些浏览器可能有不同的操作。这对所有人都有效。至于 this 与 $(this)
在函数内部,如果
this
引用触发事件的 DOM 元素,则$(this)
仍然是触发事件的 DOM 元素,只是通过将 $() 包裹在 jQuery 元素中。检查这个问题jQuery:'$(this)'和'this'之间有什么区别?
In your first example:
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'?
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.
函数(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.