简体   繁体   English

如何根据文本值显示不同的图标

[英]How to display different icon based on text value

I'm using jQuery.countdown to display multiple instances of several expiration dates on the same page and I have it set to display the number of days remaining (eg "45 days"). 我正在使用jQuery.countdown在同一页面上显示多个到期日期的多个实例,并将其设置为显示剩余天数(例如“45天”)。 I've designed it so by default there is an associated background image of "icon-conditional-rule-pass.png". 我设计了它,所以默认情况下有一个相关的背景图片“icon-conditional-rule-pass.png”。

I have the code that changes to icon-conditional-rule-fail.png if it shows 00 days, which works fine, but where I am struggling is that I want to create a period where a caution icon will display from lets say 1-60 days, I obviously will need to have "60 Days", "59 Days", etc., which is fine, I just can't figure out how (or if it's possible) to manipulate the code to work with multiple values. 我有更改为icon-conditional-rule-fail.png的代码,如果它显示00天,这很好,但我正在努力的是我想创建一个警告图标将显示的时间段让我们说1- 60天,我显然需要“60天”,“59天”等,这很好,我只是无法弄清楚如何(或者是否可能)操纵代码来处理多个值。 I'm assuming this can be done? 我假设这可以做到吗?

I've tried modifying/manipulating my code in all sorts of ways, but whenever I test, it only seems to work on the first text value and ignores the rest. 我试过以各种方式修改/操作我的代码,但每当我测试时,它似乎只对第一个文本值起作用而忽略其余的。 Searching the web yields no results to what I'm trying to achieve. 搜索网络不会产生我想要实现的结果。

$(".countdown").filter(function() {
    return $(this).text() === "00 Days";
  }).css("background-image", "url(/assets/icon-conditional-rule-fail.png)");

Want a single script that I can plug in multiple values into and have it return the desired caution image. 想要一个我可以插入多个值的脚本,让它返回所需的警告图像。 I do want to create multiple scripts (eg 1 for displaying caution 30 days before expiration, 1 for 60, etc., but I'm assuming I'll just need to change the .countdown field to something else. 我确实想要创建多个脚本(例如,1表示在到期前30天显示警告,1表示60,等等,但我假设我只需要将.countdown字段更改为其他内容。

Personally, I'd break this up into four steps: 就个人而言,我将其分为四个步骤:

  1. Create an array of ranges , which includes the number of days and the associated icon 创建ranges数组,其中包括days和关联的icon
  2. For each .countdown , parse the amount of "Days" into a number 对于每个.countdown ,将“Days”的数量解析为数字
  3. Using that number, find the corresponding range from step 1 使用该编号,从步骤1中找到相应的范围
  4. Use the icon for that range 使用该范围的图标

Not only does this leave you with one concise and reusable method, but it also allows you to update the ranges and/or icons easily by simply editing the ranges array. 这不仅为您提供了一种简洁且可重复使用的方法,而且还允许您通过简单地编辑ranges数组来轻松更新范围和/或图标。

 const ranges = [ { days: 60, //60+ days icon: "icon-for-60plus-days.png" }, { days: 30, //30-59 days icon: "icon-for-30plus-days.png" }, { days: 1, //1-29 days icon: "icon-for-1plus-days.png" }, { days: 0, //0 days icon: "icon-conditional-rule-fail.png" } ]; $(".countdown").css("background-image", function() { //For each .countdown const daysAsInt = Number($(this).text().replace(" Days", "")); //Get the days as an integer const range = ranges.find(i => daysAsInt >= i.days); //Find the range in the array return range && `url(/assets/${range.icon})`; //Set the background-image to the corresponding icon }); //FOR DEMO PURPOSES ONLY $(".countdown").each(function() { console.log($(this).text(),": ", $(this).css("background-image")); }); 
 span { display: block; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span class="countdown">70 Days</span> <span class="countdown">40 Days</span> <span class="countdown">05 Days</span> <span class="countdown">00 Days</span> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM