我想将两个变量传递给点击函数,但我不知道该怎么做。我想传递变量调用 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'
}
最佳答案
你有这个:
$(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);
});
});
关于javascript - 将变量传递给 jQuery 单击处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56530461/