如何简化Javascript动态ID

How to Simplify Javascript Dynamic IDs?

本文关键字:ID 动态 Javascript 何简化      更新时间:2023-12-14

我是JS的新手,正在运行CMS,我有一个动态ID,我担心的是,我会通过添加ID数组来简化ID以减少JS代码。

你们能帮我如何简化代码吗

$x1(document).ready(function () {
    //
    // Id1 = #options_1_text
    // Id2 = #option_2_text
    // Id3 = #option_3_text
    // Id(n) = so on..
    $x1("#options_1_text").miniColors({
        letterCase: 'uppercase',
        change: function (hex, rgb) {
            logData('change', hex, rgb);
        }
    });

    $x1("#options_2_text").miniColors({
        letterCase: 'uppercase',
        change: function (hex, rgb) {
            logData('change', hex, rgb);
        }
    });

    $x1("#options_3_text").miniColors({
        letterCase: 'uppercase',
        change: function (hex, rgb) {
            logData('change', hex, rgb);
        }
    });
    // so on..
});

非常感谢您的帮助!

谢谢!

试试这个:

$x1(document).ready(function () {
    var ids = [
        "#options_1_text",
        "#options_2_text",
        "#options_3_text",
        "#options_4_text",
        .
        .
        .
        .
        ."#options_n_text",
    ];
    $x1.each(ids, function(i, el){
        $x1(el).miniColors({
            letterCase: 'uppercase',
            change: function (hex, rgb) {
                    logData('change', hex, rgb);
            }
        });
    });
});
  var str='',n=4;
for (var i=1; i<n; i++) {
    str+=",#options_"+i+"_text";
                         }
 str[0]='';
$x1(str).miniColors({
        letterCase: 'uppercase',
        change: function (hex, rgb) {
            logData('change', hex, rgb);
        }
    });

如果代码对所有元素的作用都是一样的:

 for (var i=1; i<4; i++) {
    $x1("#options_"+i+"_text").miniColors({
                letterCase: 'uppercase',
                change: function(hex, rgb) {
                    logData('change', hex, rgb);
                }
            });
 }

如果id不是像那样的简单序列

 var ids = [ 'id1', 'another_id', 'one_more' ];

然后在该阵列上循环。

如果这是一个简单的模式,并且你不知道元素的数量n,你可以这样做:

var n = 1;
while($('#options_'+n+'_text').length > 0)
{
    $x1('#options_'+n+'_text').miniColors({
        letterCase: 'uppercase',
        change: function (hex, rgb) {
            logData('change', hex, rgb);
        }
    });
    n++;
}

这里最简单的方法是为每个项目添加一个公共类,并让jQuery为您完成所有工作,这样您就可以这样做:

$x1(document).ready(function () {
    $x1(".options_text").miniColors({
        letterCase: 'uppercase',
        change: function (hex, rgb) {
            logData('change', hex, rgb);
        }
    });
});

如果你不能修改HTML,并且你可以假设项目是从1开始按顺序编号的,你可以动态地寻找现有的元素:

$x1(document).ready(function () {
    var i = 1, elem;
    while (elem = document.getElementById("options_" + i + "_text")) {
        $x1(elem).miniColors({
             letterCase: 'uppercase',
             change: function (hex, rgb) {
                 logData('change', hex, rgb);
             }
        });
        i++;
    }
});

这里是结构化的:

$x1(document)
.ready(function () {
$x1("#options_1_text")
    .miniColors({
    letterCase: "uppercase",
    change: function (a, b) {
        logData("change", a, b)
    }
}), $x1("#options_2_text")
    .miniColors({
    letterCase: "uppercase",
    change: function (a, b) {
        logData("change", a, b)
    }
}), $x1("#options_3_text")
    .miniColors({
    letterCase: "uppercase",
    change: function (a, b) {
        logData("change", a, b)
    }
})
})