$(".1").each(function() {
if ($(this).text() == "blue") {
console.log("there");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="1">
blue
</button>
<button class="1">
1
</button>
<button class="1">
Booked
</button>
不记录任何内容,但如果我输入 1,它会记录它,而不是蓝色。
因此,它不检测字符串,以前,当我们将其与精确的字符串值进行比较时,innerHTML 的行为方式也是相同的,例如,
$(this).text() == "apple"
,它永远不会显示任何内容。
最佳答案
比较之前需要 trim 文本。
$(".1").each(function() {
if($.trim($(this).text()) == "blue") {
console.log("there");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="1">
blue
</button>
<button class="1">
1
</button>
<button class="1">
Booked
</button>
关于javascript - Jquery 不返回基本字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47150914/