比较文本和字符串jQuery

发布于 2025-02-09 13:28:19 字数 1167 浏览 1 评论 0原文

let teste = $('.btn-timer').text().trim();

  condition = '15:00'

  //let result = teste.localeCompare(condition);
  //console.log(result);

  if (teste == condition) {
      $('.btn-timer').removeClass('btn-success');
      $('.btn-timer').addClass('btn-danger');
  }

  console.log(teste);
  console.log(condition);
 .btn-danger {
        color: #991C32 !important;
        background-color: #FFB2C0 !important;
        border-color: #C82442 !important;
    }

    .btn-success {
        color: #234D31 !important;
        background-color: #B2FFCD !important;
        border-color: #449560 !important;
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="btn btn-success shadow btn-timer td-content">
  <b>15:00</b>
</button>

我试图实现一个将类()与字符串比较text()的代码:

我尝试了很多事情,但是没有运气,有什么帮助吗?

我做错了什么?以及如何填充它

let teste = $('.btn-timer').text().trim();

  condition = '15:00'

  //let result = teste.localeCompare(condition);
  //console.log(result);

  if (teste == condition) {
      $('.btn-timer').removeClass('btn-success');
      $('.btn-timer').addClass('btn-danger');
  }

  console.log(teste);
  console.log(condition);
 .btn-danger {
        color: #991C32 !important;
        background-color: #FFB2C0 !important;
        border-color: #C82442 !important;
    }

    .btn-success {
        color: #234D31 !important;
        background-color: #B2FFCD !important;
        border-color: #449560 !important;
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="btn btn-success shadow btn-timer td-content">
  <b>15:00</b>
</button>

I'am trying to implement a code that compare a text() from class with a string:

I tried a lot of things, but without luck, any help?

What I am doing wrong? and how to fiz it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长伴 2025-02-16 13:28:19

考虑以下示例,对于同一类的多个按钮。

$(function() {

  // Set Condition
  var condition = '15:00';

  $('.btn-timer').each(function(i, el) {
    // Test the element "el" Text against the Condition 
    if ($(el).text().trim() == condition) {
      // Remove class and add new class
      $(el).toggleClass('btn-success btn-danger');
    }
    console.log($(el).text().trim(), condition);
  });
});
.btn-danger {
  color: #991C32 !important;
  background-color: #FFB2C0 !important;
  border-color: #C82442 !important;
}

.btn-success {
  color: #234D31 !important;
  background-color: #B2FFCD !important;
  border-color: #449560 !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="btn btn-success shadow btn-timer td-content">
  <b>14:00</b>
</button>
<button class="btn btn-success shadow btn-timer td-content">
  <b>15:00</b>
</button>
<button class="btn btn-success shadow btn-timer td-content">
  <b>16:00</b>
</button>

使用.each()我们可以迭代每个元素。请参阅更多:

您也可以使用更直接的选择器。

$(function() {
  $(".btn-timer:contains('15:00')").toggleClass("btn-success btn-danger");
});
.btn-danger {
  color: #991C32 !important;
  background-color: #FFB2C0 !important;
  border-color: #C82442 !important;
}

.btn-success {
  color: #234D31 !important;
  background-color: #B2FFCD !important;
  border-color: #449560 !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="btn btn-success shadow btn-timer td-content">
  <b>14:00</b>
</button>
<button class="btn btn-success shadow btn-timer td-content">
  <b>15:00</b>
</button>
<button class="btn btn-success shadow btn-timer td-content">
  <b>16:00</b>
</button>

请参阅更多:

Consider the following example, for multiple buttons of the same class.

$(function() {

  // Set Condition
  var condition = '15:00';

  $('.btn-timer').each(function(i, el) {
    // Test the element "el" Text against the Condition 
    if ($(el).text().trim() == condition) {
      // Remove class and add new class
      $(el).toggleClass('btn-success btn-danger');
    }
    console.log($(el).text().trim(), condition);
  });
});
.btn-danger {
  color: #991C32 !important;
  background-color: #FFB2C0 !important;
  border-color: #C82442 !important;
}

.btn-success {
  color: #234D31 !important;
  background-color: #B2FFCD !important;
  border-color: #449560 !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="btn btn-success shadow btn-timer td-content">
  <b>14:00</b>
</button>
<button class="btn btn-success shadow btn-timer td-content">
  <b>15:00</b>
</button>
<button class="btn btn-success shadow btn-timer td-content">
  <b>16:00</b>
</button>

Using .each() we can iterate each element. See More:

You can also use a more direct Selector.

$(function() {
  $(".btn-timer:contains('15:00')").toggleClass("btn-success btn-danger");
});
.btn-danger {
  color: #991C32 !important;
  background-color: #FFB2C0 !important;
  border-color: #C82442 !important;
}

.btn-success {
  color: #234D31 !important;
  background-color: #B2FFCD !important;
  border-color: #449560 !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="btn btn-success shadow btn-timer td-content">
  <b>14:00</b>
</button>
<button class="btn btn-success shadow btn-timer td-content">
  <b>15:00</b>
</button>
<button class="btn btn-success shadow btn-timer td-content">
  <b>16:00</b>
</button>

See More:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文