编写此 jquery 函数的更简洁或更简单的方法?

时间:2021-03-20 00:59:34

标签: jquery

def fnct_to_opt(t, k2, k1):
    #EXPERIMENTAL CONSTANTS
    v = 105
    Co = 1500

    A = (-np.log((k1/v)*Co))/k2
    print("TRACE", "\nk2", k2, "\nt", t, "\nA", A, "\nother", k1, v, Co)
    return ( (np.exp(-k2*(t+A))) - ((k1/v)*Co) )/ -k2
  1. 这是根据同一行中另一列中的值对 mvc 网格列进行样式更改的正确方法吗?
  2. 是否有更好/更简洁或更简单的方法来实现类似的功能?

1 个答案:

答案 0 :(得分:1)

使用由文本索引的对象来查找,其值是要设置的颜色:

const colorsByText = {
    1: 'green',
    2: 'yellow',
    3: 'red'
};
function foo() {
    $('#bar > table > tbody > tr').each(function () {
        const text = $(this).find('td:nth-child(3)').text();
        const color = colorsByText[text];
        if (color) {
            $(this).find('td:nth-child(1)').css('background-color', color);
        }
    });
}

如果文本始终是这 3 个属性之一,您可以删除 if 检查 - 或者,更好的是,使用数组而不是对象:

const colors = ['green', 'yellow', 'red'];
function foo() {
    $('#bar > table > tbody > tr').each(function () {
        const text = $(this).find('td:nth-child(3)').text();
        const color = colors[text - 1];
        $(this).find('td:nth-child(1)').css('background-color', color);
    });
}
相关问题