gpt4 book ai didi

javascript - Jquery 变量无法正确比较

转载 作者:行者123 更新时间:2023-12-03 11:56:02 25 4
gpt4 key购买 nike

我试图从这 3 个元素中获取结果,每次单击按钮时它都会更新

<span id="machine1Result" class="slotMachine noBorder">0</span>
<span id="machine2Result" class="slotMachine noBorder">0</span>
<span id="machine3Result" class="slotMachine noBorder">0</span>

使用此代码尝试获取元素的 3 个组合值,如果匹配,我可以进行一些更改:

var text = $('#machine1Result,#machine2Result,#machine3Result').text();
var comparingText0 = "000";
var comparingText1 = "111";

if (text == comparingText0) {
$('.wonbg').css({'background-image': 'url(img/won_bg.png)'});
$('.rw14').css({'backgroundColor': '#ff9900'});
count += 1;
}
if (text == comparingText1) {
$('.wonbg').css({'background-image': 'url(img/won_bg.png)'});
$('.rw1').css({'backgroundColor': '#ff9900'});
count += 1;
}
else {
$('.slotMachineButton').click(function() {

$('.wonbg').css({"background": "none"});
$('.rewards').css({"background": "none"});

});
}

如果元素具有相同的数字,上面的代码可以正确运行条件,但即使有一个错误的数字,例如 110 或 101,它也能正常运行,反之亦然,尝试 === 给我相同的结果结果。

最佳答案

如果第一个条件为真,它也会触发其他条件。那是因为这一行

if ( text == comparingText1 ) { 

应该是 else if

else if ( text == comparingText1 ) { 

现在所有条件都将链接起来。

另一个问题是文本是text()

var text = $('#machine1Result,#machine2Result,#machine3Result').text();

编辑:由于您给出的内容是错误的,我必须更改答案......

text)_ 只会给出第一个答案,因此您需要循环遍历它们

var results = $('#machine1Result,#machine2Result,#machine3Result');
results.each( function() {
var currentLine = $(this);
var currentText = currentLine.text();
console.log(currentText);
} );

关于javascript - Jquery 变量无法正确比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25614101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com