有三个函数,一个是总体函数,然后是两个函数,它们由toggle事件触发。我希望第二个函数能够获取值并将其传递给第二个函数。
function(){
$('selector').toggle(
//i want this to gather a value and store it in a variable
function(){ },
//and i want this to accept the variable and value from the previous function
function(){}
)}
答案 0 :(得分:0)
您可以使用jQuery Data(http://api.jquery.com/jQuery.data/)在选择器匹配项中存储每个元素的值,以存储和检索值。
EXA:
function(a,b){
$('selector').toggle(
//i want this to gather a value and store it in a variable
function(){
$(this).data("<YOUR_VARIABLE_NAME>", <YOUR_VARIABLE_VALUE>);
},
//and i want this to accept the variable and value from the previous function
function(){
var someVariable = $(this).data("<YOUR_VARIABLE_NAME>");
}
)}
答案 1 :(得分:0)
function(){
// this var will be in scope for anything inside this function
var availableInBoth;
$('selector').toggle( function(){
// this var is only usable in this function
var availableHereOnly = 10;
availableInBoth = 10;
}, function(){
console.log(availableInBoth);
})
}