Function 如何从单击事件返回函数?

Function 如何从单击事件返回函数?,function,events,click,return,Function,Events,Click,Return,我有一个click事件,在事件处理程序中有一个函数,它假设返回一个字符串值 例如: $('.customDropDownList li').click(function(){ var yourCurrentSeasonSelection = selectionReplaced(this); //return var yourCurrentSeasonSelection from here. }); function selectionReplaced(r

我有一个click事件,在事件处理程序中有一个函数,它假设返回一个字符串值

例如:

  $('.customDropDownList li').click(function(){

      var yourCurrentSeasonSelection = selectionReplaced(this);

      //return var yourCurrentSeasonSelection from here.
  });



function selectionReplaced(refT){   
   var valueRegistered = $(refT).find("a[href]").attr('href').replace('#', '');
    ....
   return valueRegistered;
}
如何从yourCurrentSeasonSelection变量中获取单击后的返回值?

在单击实际发生之前,您不能“返回”yourCurrentSeasonSelection。要使用该值执行的任何操作都必须进入单击处理程序内部

$('.customDropDownList li').click(function() {
    var yourCurrentSeasonSelection = selectionReplaced(this);

    //do something with yourCurrentSeasonSelection
});

事件没有返回值。不过你可以这样做

$(".customDropDownList li").click(function(event){
  selectionReplaced(this);
  event.preventDefault();
});

function selectionReplaced(refT){   
  var link = $(refT).find("a[href]");

  link.attr("href", function(idx, href){
    return href.replace("#", ");
  });
}

您可以将其传递给另一个执行视图逻辑的函数。或者,您可以只完成单击函数中的逻辑。您可能希望返回false;要在执行所需操作后禁用默认的单击功能