javascript - Jquery 不返回基本字符串

标签 javascript jquery string

$(".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/

相关文章:

javascript - re构造javascript数组

asp.net - 在 iframe 中查找组件

javascript - 检查未定义的值不起作用?

Javascript 四舍五入到最接近的 0.5

javascript - Fancybox 出现问题 (P

string - 当目标是找到特定字符串的所有出现时,KMP 的最坏情况复杂度是多少?

python - 为什么我不能在一个类中连接 'str' 和 'instance' 对象?

jquery - 使用 jQuery 进行内联编辑

jquery - Bootstrap - 如何仅从折叠的导航栏中删除悬停效果?

java - List<string> 包含此的项目