jquery函数上的点符号?

时间:2021-08-04 22:29:29

I came across this in a jQuery book:

我在一本jQuery书中看到了这个:

$(elems).mouseenter(function(e) {
           $(this).css("opacity", 0.5);
        }).mouseout(function(e) {
           $(this).css("opacity", 1.0);
        })

I removed much of the code for easier reading and then got this:

我删除了大部分代码以便于阅读,然后得到了这个:

$(elems).mouseenter(function(e)).mouseout(function(e))

It seems like in general you can do this?:

看起来一般情况下你可以做到这一点?:

$(elems).mouseenter(function(e)).mouseout(function(e)).mouseSOMETHING1(function(e))

Another words using the . to concatenate functions?

使用的另一个词。连接函数?

Also if I broke this code into say:

如果我打破这段代码说:

$(elems).mouseenter(function(e) {$(this).css("opacity", 0.5);});
$(elems).mouseout(function(e) {$(this).css("opacity", 1.0);});

Is this the same?

这是一样的吗?

Thanks, Jim

谢谢,吉姆

4 个解决方案

#1


2  

It works because all those function returns jQuery object.

它工作,因为所有这些函数返回jQuery对象。

   $(elems)  //return jquery object
     .mouseenter(/* ...*/)  //return jquery object
     .mouseout(/* ..*/)     //return jquery object

by this way you can chain as many functions.

通过这种方式,您可以链接尽可能多的功能。

$(elems).mouseenter(function(e) {$(this).css("opacity", 0.5);});
$(elems).mouseout(function(e) {$(this).css("opacity", 1.0);});

both methods are functionally same.. except that the 2nd method makes an unnecessary jQuery function call to get $(elems).

两个方法在功能上都相同..除了第二个方法进行不必要的jQuery函数调用以获取$(elems)。

#2


6  

Yes, it is. One of the key features of jQuery is precisely chainability. This is done by returning the jQuery object itself in almost every call, allowing you to pass it along to the next method of the chain.

是的。 jQuery的一个关键特性就是可链接性。这是通过在几乎每次调用中返回jQuery对象本身来完成的,允许您将它传递给链的下一个方法。

#3


2  

What you are talking about is called method chaining and yes, it is well supported in jQuery. Take a look at this quick walkthrough:

你所谈论的是称为方法链接,是的,它在jQuery中得到了很好的支持。看看这个快速演练:

Method chaining - The complete jQuery tutorial

方法链 - 完整的jQuery教程

#4


1  

Yes $(elems).mouseenter(function(e)).mouseout(function(e)).mouseSOMETHING1(function(e)) is generally acceptable because the jquery functions return the jquery object in question. When you write it out as :

是$(elems).mouseenter(function(e))。mouseout(function(e))。mouseSOMETHING1(function(e))通常是可以接受的,因为jquery函数返回有问题的jquery对象。当你把它写成:

$(elems).mouseenter(function(e) {$(this).css("opacity", 0.5);});
$(elems).mouseout(function(e) {$(this).css("opacity", 1.0);});

It is definitely equivalent to writing it as $(elems).mouseenter().mouseout()

它绝对等同于将其写为$(elems).mouseenter()。mouseout()

#1


2  

It works because all those function returns jQuery object.

它工作,因为所有这些函数返回jQuery对象。

   $(elems)  //return jquery object
     .mouseenter(/* ...*/)  //return jquery object
     .mouseout(/* ..*/)     //return jquery object

by this way you can chain as many functions.

通过这种方式,您可以链接尽可能多的功能。

$(elems).mouseenter(function(e) {$(this).css("opacity", 0.5);});
$(elems).mouseout(function(e) {$(this).css("opacity", 1.0);});

both methods are functionally same.. except that the 2nd method makes an unnecessary jQuery function call to get $(elems).

两个方法在功能上都相同..除了第二个方法进行不必要的jQuery函数调用以获取$(elems)。

#2


6  

Yes, it is. One of the key features of jQuery is precisely chainability. This is done by returning the jQuery object itself in almost every call, allowing you to pass it along to the next method of the chain.

是的。 jQuery的一个关键特性就是可链接性。这是通过在几乎每次调用中返回jQuery对象本身来完成的,允许您将它传递给链的下一个方法。

#3


2  

What you are talking about is called method chaining and yes, it is well supported in jQuery. Take a look at this quick walkthrough:

你所谈论的是称为方法链接,是的,它在jQuery中得到了很好的支持。看看这个快速演练:

Method chaining - The complete jQuery tutorial

方法链 - 完整的jQuery教程

#4


1  

Yes $(elems).mouseenter(function(e)).mouseout(function(e)).mouseSOMETHING1(function(e)) is generally acceptable because the jquery functions return the jquery object in question. When you write it out as :

是$(elems).mouseenter(function(e))。mouseout(function(e))。mouseSOMETHING1(function(e))通常是可以接受的,因为jquery函数返回有问题的jquery对象。当你把它写成:

$(elems).mouseenter(function(e) {$(this).css("opacity", 0.5);});
$(elems).mouseout(function(e) {$(this).css("opacity", 1.0);});

It is definitely equivalent to writing it as $(elems).mouseenter().mouseout()

它绝对等同于将其写为$(elems).mouseenter()。mouseout()