简体   繁体   English

嵌套函数上未定义的$(this)

[英]Undefined $(this) on nested functions

So basically I have two functions on my click() trigger. 因此,基本上,我的click()触发器具有两个功能。

var firstFunction = function() {
    var id = $(this).attr('id');
    alert(id);
};

var secondFunction = function() {
    //something here
};

$('#trigger').click(function() {
    firstFunction();
    secondFunction();
});

On firstFunction() I'm trying to get $(this).attr('id') but it's returning undefined . firstFunction()我试图获取$(this).attr('id')但它返回的是undefined

I know it has something two do with calling multiple functions because it works when I only call one function 我知道它与调用多个函数有两点关系,因为当我仅调用一个函数时它就起作用

$('#trigger').click(firstFunction);

Sample Fiddle here 样品小提琴在这里

As per your existing approach this refers to Window object not the element which invoke the event. 按照您现有的方法, this是指Window对象而不是调用事件的元素。

You can use .bind() 您可以使用.bind()

The bind() method creates a new function that, when called, has its this keyword set to the provided value, bind()方法创建一个新函数,该函数在被调用时将其this关键字设置为提供的值,

$('#trigger').click(function() {
    firstFunction.bind(this)();
    secondFunction.bind(this)();
})

 var firstFunction = function() { var id = $(this).attr('id'); console.log(id); }; var secondFunction = function() { //something here var id = $(this).attr('id'); console.log(id); }; $('#trigger').click(function() { firstFunction.bind(this)(); secondFunction.bind(this)(); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="trigger">Click Me</button> 

Fiddle 小提琴

It's returning undefined because you aren't applying the same this as the event. 它返回的是undefined,因为您未将其与this事件应用相同。 You can achieve this by using call or apply instead of calling it directly. 您可以通过使用callapply而不是直接call来实现。

$('#trigger').click(function() {
    firstFunction.call(this);
    secondFunction.call(this);
});

The this inside the firstFunction will be the window object itself - pass this to the function to fix it - see demo below: this里面firstFunction窗口对象本身-通过this修复它的功能-见下面的演示:

 var firstFunction = function(el) { var id = $(el).attr('id'); alert(id); }; var secondFunction = function() { //something here }; $('#trigger').click(function() { firstFunction(this); secondFunction(this); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="trigger">Click Me</button> 

Another way is to use Function.prototype.call to bind a this argument to the funciton: 另一种方法是使用Function.prototype.callthis参数绑定到Function.prototype.call上:

 var firstFunction = function() { var id = $(this).attr('id'); alert(id); }; var secondFunction = function() { //something here }; $('#trigger').click(function() { firstFunction.call(this); secondFunction.call(this); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="trigger">Click Me</button> 

You can pass the jQuery object element and than only use it in your function: 您可以传递jQuery对象元素,而不是仅在函数中使用它:

 var firstFunction = function($el) { var id = $el.attr('id'); console.log(id); }; var secondFunction = function() { //something here }; $('#trigger').click(function() { firstFunction($(this)); secondFunction(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="trigger">Button</button> 

click() handler receives event as parameter. click()处理函数将事件作为参数接收。 Pass it because this is not available in the scope of firstFunction() . 通过它,因为this是不可用的范围firstFunction()

Like this: 像这样:

var firstFunction = function(target) {
    var id = $(target).attr('id');
    alert(id);
};

var secondFunction = function() {
    //something here
};

$('#trigger').click(function(e) {
    firstFunction(e.target);
    secondFunction();
});

Nothing to do major, it's quite simple. 不需要专业,这很简单。

If you are calling a function and into that function you need an event to be used, then pass a reference in the function parameter. 如果要调用一个函数并在该函数中调用,则需要使用一个事件,然后在function参数中传递一个引用。

I have updated the code in your Fiddle and updated with Mine: 我已经更新了您的Fiddle中的代码,并使用Mine更新了:

Step 1: 第1步:

$('#trigger').click(function(event) {
  var that = this;
  firstFunction(that);
  secondFunction(that);
});

Created a variable that assigned this to that and passed into function parameter. 创建一个变量分配给该变量并传递给函数参数。

Note that, writing event in the click function is required to define this as a local click reference (Not a window) 请注意,需要在click函数中写入事件才能将其定义为本地Click引用(不是窗口)

Step 2: 第2步:

Passed that rather than this, to make sure the reference is from click function only not of window. 通过了这个而不是这个,以确保引用仅来自单击功能而不是窗口。

var firstFunction = function(that) {
  var id = $(that).attr('id');
  alert(id);
};

Updated Fiddle 更新小提琴

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

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