简体   繁体   English

将$(this)选择器传递给嵌套函数

[英]Passing $(this) selector into nested functions

So I have a script in place that needs to go through each P tag within a parent DIV with the class name of entry-content and translate each one using the google translate API. 所以我有一个脚本,需要在父DIV使用entry-content的类名来遍历每个P标记,并使用google translate API翻译每个标记。

So when a user clicks a link to translate the page from english to spanish this function is run: 因此,当用户单击链接以将页面从英语翻译为西班牙语时,将运行以下功能:

function spanish() {
$(".entry-content p").each(function(){
      var text = $(this).html();
        google.language.detect(text, function(result) {
          google.language.translate(text, "en", "es", function(result) {
           if (result.translation) {
                alert($(this).html());  //outputs NULL
                $(this).html(result.translation); //doesn't work
            }
          });
        });
      });
}

The problem is when iIget to the inner function $(this).html() comesback NULL and I am not able to change the current elements html in order to change it to the new translated text. 问题是当iIget到内部函数$(this).html()回复NULL并且我无法更改当前元素html以便将其更改为新的翻译文本。

So I guess my question is: How would I pass the current selected element into the nested functions? 所以我想我的问题是:如何将当前选中的元素传递给嵌套函数?

Thanks 谢谢

You may store it in a local variable 您可以将其存储在本地变量中

The value of this will always relate to the context in which the function is called. this的值将始终与调用函数的上下文相关。 In your example, you're passing a function to google.language.translate , and so presumably, it is google.language.translate that calls that function. 在您的示例中,您将函数传递给google.language.translate ,因此可能是调用该函数的google.language.translate

However, if you store the value of $(this) when it is your p , you will be able to use that variable from the callback function. 但是,如果在它是p时存储$(this)的值,则可以使用回调函数中的该变量。

function spanish() {
    $(".entry-content p").each(function(){
      var $this = $(this);
      var text = $this.html();
        google.language.detect(text, function(result) {
          google.language.translate(text, "en", "es", function(result) {
           if (result.translation) {
                alert($this.html());  //outputs NULL
                $this.html(result.translation); //doesn't work
            }
          });
        });
      });
}

It's because this changes context in that callback, just keep a reference to the element/object you want, like this: 这是因为this改变该回调中的上下文,只需保留对所需元素/对象的引用,如下所示:

function spanish() {
  $(".entry-content p").each(function(){
    var text = $(this).html(), self = this;
    google.language.detect(text, function(result) {
      google.language.translate(text, "en", "es", function(result) {
       if (result.translation) {
            $(self).html(result.translation);
        }
      });
    });
  });
}

Save a record of 'this' in the proper context - otherwise this refers to the inner function, not the parent function. 在适当的上下文中保存'this'的记录 - 否则this是指内部函数,而不是父函数。

function spanish() {
$(".entry-content p").each(function(){

      // Save a record of 'this' in the proper context.
      var me = this;

      var text = $(this).html();
        google.language.detect(text, function(result) {
          google.language.translate(text, "en", "es", function(result) {
           if (result.translation) {
                alert($(me).html());  
                $(me).html(result.translation); 
            }
          });
        });
      });
}

I would do something like this. 我会做这样的事情。 Then research closures to understand what it's doing. 然后研究闭包以了解它正在做什么。

function spanish() {
$(".entry-content p").each(function(){
var $this = this; // new
      var text = $(this).html();
        google.language.detect(text, function(result) {
          google.language.translate(text, "en", "es", function(result) {
           if (result.translation) {
                alert($(this).html());  //outputs NULL
                $this.html(result.translation); 
            }
          });
        });
      });
}

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

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