简体   繁体   English

jQuery事件中的自调用函数不起作用

[英]Self invoking function inside jQuery event is not working

Self invoking function defined inside jQuery event is not working, but why? 在jQuery事件中定义的自调用函数不起作用,但为什么呢?

 $('div').on('click', function(){ $('div').text($('div').text() + 1) (function(){ $('div').text($('div').text() + 0) })(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>text</div> 

Edit: Answers below are focused on this keyword so I've changed this parameter to 'div' . 编辑:下面的答案专注this关键字,所以我已this参数更改为'div' It still does not work. 它仍然无法正常工作。

Inside that IIFE the this refers to another context. IIFE内部, this指的是另一个背景。 Each function has it's own context. 每个函数都有自己的上下文。 You can use arrow function , explicitly binding of the context or just keep this reference into another variable and use it. 您可以使用箭头函数显式绑定上下文或仅this引用保留到另一个变量中并使用它。 One more thing, you missed to put ; 还有一件事,你错过了; after the first statement, this will cause an error. 在第一个语句之后,这将导致错误。

Also don't use this style in that code $('div') , this will find all divs but get the text of the first one, so you do more job than it needs. 也不要在代码$('div')使用这种风格,这将找到所有div但得到第一个的文本,所以你做的工作超出了它的需要。

Arrow function 箭头功能

 $('div').on('click', function() { $(this).text($(this).text() + 1); (() => { $(this).text($(this).text() + 0) })(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>text</div> 

Explicit binding of the context 显式绑定上下文

 $('div').on('click', function(){ $(this).text($(this).text() + 1); (function() { $(this).text($(this).text() + 0) }).bind(this)(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>text</div> 

Keep the reference into another variable 将引用保留在另一个变量中

 $('div').on('click', function(){ const that = this; $(that).text($(that).text() + 1); (function(){ $(that).text($(that).text() + 0) })(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>text</div> 

Your problem is that you miss a ; 你的问题是你错过了; at the end of $('div').text($('div').text() + 1) $('div').text($('div').text() + 1)的末尾$('div').text($('div').text() + 1)

Without the ; 没有; it is the same as if you would write: 它和你写的一样:

$('div').text($('div').text() + 1)(function(){
   $('div').text($('div').text() + 0)
})();

But because text($('div').text() + 1) does not return a function, you will get this error. 但是因为text($('div').text() + 1)没有返回函数,所以会出现这个错误。

Uncaught TypeError: $(...).text(...) is not a function 未捕获的TypeError:$(...)。text(...)不是函数

This is one situation where you have to use the ; 这是你必须使用的一种情况; to end your statement. 结束你的陈述。

ecma-262: 11.9.2 Examples of Automatic Semicolon Insertion ecma-262:11.9.2自动分号插入的例子

The source 来源

 a = b + c (d + e).print() 

is not transformed by automatic semicolon insertion, because the parenthesised expression that begins the second line can be interpreted as an argument list for a function call: 自动分号插入不会转换,因为从第二行开始的括号表达式可以解释为函数调用的参数列表:

 a = b + c(d + e).print() 

So you have to write: 所以你必须写:

$('div').text($('div').text() + 1);
(function(){
   $('div').text($('div').text() + 0)
})();

 $(document).ready(function(){ $('div').on('click', function(){ $(this).text($(this).text() + 1); var divelement = $(this); (function(element){ element.text($(element).text() + 0); })(divelement); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>text</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM