我想将两个变量传递给click函数,但是我不知道该怎么做。我想传递变量调用stringUrl和stringUrl2,但控制台告诉我变量未定义。
第一个功能:
$('.fancy .slot').jSlots({
number : 1,
winnerNumber : 1,
spinner : '#playFancy',
easing : 'swing',
time : 1000,
loops : 1,
onStart : function() {
$('.slot').removeClass('winner');
},
onWin : function(winCount, winners, finalNumbers) {
},
onEnd : function(winCount, winners, finalNumbers) {
var selector = $('.fancy .slot li:nth-child(' +winCount[0])
var stringUrl = selector.css('background-image');
stringUrl = stringUrl.replace('url(','').replace(')','');
stringUrl = stringUrl.substr(41);
alert(stringUrl);
}
});
第二个功能:
$('.fancy2 .slot2').jSlots({
number : 1,
winnerNumber : 1,
spinner : '#playFancy2',
easing : 'easeOutSine',
time : 1000,
loops : 1,
onStart : function() {
$('.slot2').removeClass('winner');
},
onWin : function(winCount, winners, finalNumbers) {
},
onEnd : function(winCount, winners, finalNumbers) {
var selector = $('.fancy2 .slot2 li:nth-child(' +winCount[0])
var stringUrl2 = selector.css('background-image');
stringUrl2 = stringUrl2.replace('url(','').replace(')','');
stringUrl2 = stringUrl2.substr(41);
alert(stringUrl2);
}
});
我需要变量的函数:
$('#btnConfirm').click(function(){
console.log(stringUrl);
console.log(stringUrl2);
if(stringUrl){
Swal.fire({
background: 'no-repeat center url(/start/assets/img/rouletteArt/popup-bravo.png)',
showConfirmButton : true,
confirmButtonClass : 'placeButton',
confirmButtonColor: '#253654',
animation: false,
customClass: {
popup: 'animated tada'
}
答案 0 :(得分:1)
你有这个:
$(document).ready(function(){
$('.fancy .slot').jSlots({
//stuff
var stringUrl = selector.css('background-image');
stringUrl = stringUrl.replace('url(','').replace(')','');
stringUrl = stringUrl.substr(41);
});
$('.fancy2 .slot2').jSlots({
//stuff
var stringUrl2 = selector.css('background-image');
stringUrl2 = stringUrl2.replace('url(','').replace(')','');
stringUrl2 = stringUrl2.substr(41);
});
$('#btnConfirm').click(function(){
alert(stringUrl);
alert(stringUrl2);
});
});
您想要这个:
$(document).ready(function(){
var stringUrl, stringUrl2; //<=====================
$('.fancy .slot').jSlots({
//stuff
stringUrl = selector.css('background-image'); //<==============
stringUrl = stringUrl.replace('url(','').replace(')','');
stringUrl = stringUrl.substr(41);
});
$('.fancy2 .slot2').jSlots({
//stuff
stringUrl2 = selector.css('background-image'); //<==============
stringUrl2 = stringUrl2.replace('url(','').replace(')','');
stringUrl2 = stringUrl2.substr(41);
});
$('#btnConfirm').click(function(){
alert(stringUrl);
alert(stringUrl2);
});
});
答案 1 :(得分:1)
您可以尝试使用bind()
:
onEnd: function(stringUrl, winCount, winners, finalNumbers) {
}.bind(null, stringUrl)
当您绑定这样的变量时,回调将在执行时将其作为默认参数之前的参数接收。这种做法可能会有一些弊端,但是,如果其他范围建议失败,那么这可能是您的解决方案,因为您的代码非常简单。