input框根据数值范围字体颜色显示不同

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Input Value Color Change</title>
    <style>
        /* Define your color classes as before */
        .input-color-red {
            color: red;
        }
        .input-color-green {
            color: green;
        }
        .input-color-blue {
            color: blue;
        }
        /* Add more classes for other colors as needed */
    </style>
</head>
<body>

<!-- Assume you have 30 input boxes with IDs "input1" to "input30" -->
<input type="text" id="input1" value="100">
<input type="text" id="input2" value="10">
<!-- ... and so on for the remaining inputs -->

<script>
    const inputRangeColors = [
        {
            id: 'TuoyanAI01',
            ranges: [
                { min: 0, max: 29.99, className: 'input-color-yellow' },
                { min: 30, max: 39.99, className: 'input-color-green' },
                { min: 40, max: Infinity, className: 'input-color-red' }
            ]
            id: 'input1',
            ranges: [
                { min: 0, max: 9, colorClass: 'input-color-red' },
                { min: 10, max: 19, colorClass: 'input-color-green' },
                { min: 20, max: Infinity, colorClass: 'input-color-blue' }
            ]
        },
        {
            id: 'input2',
            ranges: [
                { min: -5, max: 4, colorClass: 'input-color-red' },
                { min: 5, max: 14, colorClass: 'input-color-green' },
                { min: 15, max: Infinity, colorClass: 'input-color-blue' }
            ]
        },
    ];

    function applyInputColor(input, ranges) {
        const value = parseFloat(input.value);
        
        input.classList.remove(...ranges.flatMap(range => [range.colorClass]));
        
        const matchingRange = ranges.find(range => value >= range.min && value <= range.max);
        if (matchingRange) {
            input.classList.add(matchingRange.colorClass);
        }
    }
    
    inputRangeColors.forEach(({ id, ranges }) => {
        const input = document.getElementById(id);
        applyInputColor(input, ranges);

        input.addEventListener('input', function () {
            applyInputColor(this, ranges);
        });
    });
</script>
</body>
</html>
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值