Javascript 有没有办法转移';这';到一个不同的功能?

Javascript 有没有办法转移';这';到一个不同的功能?,javascript,jquery,this,Javascript,Jquery,This,我有一个实例,需要将this的值转移到另一个函数中使用。显而易见的解决方案是使用全局变量,但我听说这不是一个好做法。这是我的密码: $('.title').each(function(){ $(this).click(function(){ titleThis = this; }); }); $('.post-footer').click(function(){ $(titleThis).css('background','red'); }); 如何在

我有一个实例,需要将
this
的值转移到另一个函数中使用。显而易见的解决方案是使用全局变量,但我听说这不是一个好做法。这是我的密码:

$('.title').each(function(){
   $(this).click(function(){
       titleThis = this;
   });  
});

$('.post-footer').click(function(){
    $(titleThis).css('background','red');
});

如何在不使用全局变量的情况下执行此操作?

旁注:我不能这样做:

$('.title').each(function(){
       $(this).click(function(){
           var titleThis = this;
           $('.post-footer').click(function(){
               $(titleThis).css('background','red');
           });
       });  
    });
因为我在本例中使用的插件代替了
。click()
(JQuery航路点)在我尝试堆叠它时会抛出一个错误(不确定原因)。在我的第一个示例中,当我使用全局变量时,它工作得很好,如果可能的话,我只想避免使用全局变量

编辑: 由于我的示例代码中似乎存在一些问题,一些解决方案在我的实际代码中不起作用,下面是我的实际代码块:

    $('.title').each(function() {
            //creating the waypoint
          $(this).waypoint(function(direction) {
//declaring global variable as this
              titleThis = this;
              //if below waypoint, do animation
              if (direction === "down"){
                  //fix the title
                  $(this.element).addClass('titleFixed');
                  //slide right
                  $(this.element).animate({left:'10%'},250,function(){
                      //slide title decoration right
                      $(this).next().animate({left:'0'},50);
                  });
                  //if above waypoint, undo animation
              }else{
                  //unfix the title
                  $(this.element).removeClass('titleFixed');
                  //animate left
                  $(this.element).animate({left:'-3.5%'},240,function(){
                      //animate title direction left
                    $(this).next().animate({left:'-3.5%'},150);
                  });
              }
          });
        });
      $('.post-footer').waypoint(function() {
         $(titleThis.element).css('background','red');
      });

如果我尝试将第二个.waypoint函数嵌套在第一个函数中,我会得到一个错误。

您不需要全局变量。只需将代码包装在一个自动执行函数中:

(函数(){
var titleThis;
$('.title')。单击(函数(){
titleThis=这个;
});
$('.post footer')。单击(函数(){
$(titleThis.css('background','red');
});
})();
//“titleThis”在这里没有定义。

必要时使用全局变量没有什么错。错误的是在传递值(或引用)作为函数参数时过度使用全局变量更合适。但情况并非如此,因为$(.post footer)上的单击处理程序是独立触发的,并且不与$(.title)的单击处理程序共享变量范围

不清楚您试图用代码实现什么。但是使用一个全局变量来标记上一次单击哪个div是完全正确的。如果这就是你射击的目的

我建议首先声明和定义全局变量:

var titleThis = null;

您还可以尝试只使用css并向目标添加一个.selected类

$('.title').each(function(){
   $(this).click(function(){
       $('.title.selected').removeClass('selected'); // Prevents having multiples selected.
       $(this).addClass('selected')
   });  
});

$('.post-footer').click(function(){
    $('.title').css('background', ''); // Undo css on previous titles
    $('.title.selected').css('background','red');
});

您实际想要实现什么(从功能角度,而不是“此”上下文角度)?这将使我们能够提供更好的洞察力。使用JQuery航路点,我试图在到达
.post footer
航路点时,使当前活动的
.title
变为红色。因为所有的标题只有一个类,所以我需要使用<代码> < <代码>来选择当前的一个。您可以考虑只在单击标题标题时添加单击处理程序,然后单击后页脚删除它。这将允许您避免全局变量。我以前从未遇到过这种情况!奇怪。无论如何谢谢你,蒂尔!“
$(.post footer)
[…]上的单击处理程序与
$(.title)
的单击处理程序不共享变量范围。”。然后可以创建该范围,而不是使用全局范围。