我有一个嵌套函数,它失去了“this”的正确范围
if(tabNav === true){
$("." + tabNavClass).show();
tabNavItem.first().addClass("active");//active class on first li
tabNavItem.each(function(i){
$(this).attr("rel",(i + 1));
});
tabNavItem.on("click", function(e){
tabNavClick();
//this works, but i don't want it defined here. i want it in the tabNavClick() function
//var data = $(this).attr("rel");
//console.log(data);
e.preventDefault();
});
}
//tab nav function
function tabNavClick(){
// REGISTERS UNDEFINED HERE B/C LOSES SCOPE OF "THIS". FIX?!
var that = $(this);
var data = that.attr("rel");
console.log(data);
tabNavItem.removeClass("active");//remove existing active class
};
我试过var that = $(this)
了,但这不起作用,我不认为 .call 在这种情况下会起作用。我如何$(this).attr("rel");
返回我想要的 rel # 值而不是undefined
?