gpt4 book ai didi

jquery - 根据百分比值更改 CSS 类

转载 作者:行者123 更新时间:2023-11-28 03:43:30 25 4
gpt4 key购买 nike

Here's the latest JSFiddle .

HTML:

<table id="math-table">
<tr>
<td><input type="text" id="A1" name="A" value=""></td>
<td><input type="text" id="B1" name="B" value=""></td>
<td><input type="text" id="C1" name="C" readonly="readonly" tabIndex="-1" value=""></td>
</tr>
</table>

JS:

$("#math-table input").live("keyup", function(){
var id = this.id.match(/\d+/);
$("#C"+id).val( Math.round (($("#A"+id).val() / $("#B"+id).val()) * 100) + "%" );

$('#A'+id).attr('value', $('#A'+id).val());
$('#B'+id).attr('value', $('#B'+id).val());
$('#C'+id).attr('value', $('#C'+id).val());
});

var uniqueIds = $("#math-table tr").length;
$("#math-table input[id^='B']").live("change", function(){
var $thisRow = $(this).closest("tr"),
$clone = $thisRow.clone(), // Clone row
$inputs = $clone.find("input").val("");// Reset values, return all inputs
uniqueIds++; //Increment ID
$inputs[0].id = "A" + uniqueIds;
$inputs[1].id = "B" + uniqueIds;
$inputs[2].id = "C" + uniqueIds;
$thisRow.after($clone);
});

您可以看到 A/B = C% 非常简单。我如何根据特定的百分比将不同的 CSS 类添加到 C 中?

红色 1-33%

绿色 34-66%

蓝色 67-100%

最佳答案

使用addClass , removeClass 函数来改变。如下所示:

$(".first").addClass("second") // add a class

$(".first").removeClass("second") // remove a class

只需将 C 值放入一个变量中,然后通过应用 if/else 根据您想要的 % 更改类

关于jquery - 根据百分比值更改 CSS 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8718996/

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